Retrieve <TITLE> in php code ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Info 3000

    Retrieve <TITLE> in php code ?

    Hi there,

    Simple question, but headache !

    I have a PHP Page like :

    <HTML>
    <HEAD>
    <TITLE>Sunny day !</TITLE>
    </HEAD>
    <BODY>
    <?
    echo "The title of this page please (Sunny Day !)"
    ?>
    </BODY>
    </HTML>

    I think that you have understanding my problem : How to retrieve the
    title of my page ?

    Very very thanks in advance !

    Michel
  • Pedro Graca

    #2
    Re: Retrieve &lt;TITLE&gt ; in php code ?

    Info 3000 wrote:[color=blue]
    > I have a PHP Page like :
    >
    ><HTML>
    ><HEAD>
    > <TITLE>Sunny day !</TITLE>
    ></HEAD>
    ><BODY>
    > <?
    > echo "The title of this page please (Sunny Day !)"
    > ?>
    ></BODY>
    ></HTML>
    >
    > I think that you have understanding my problem : How to retrieve the
    > title of my page ?[/color]

    I'd look at it differently:

    <html>
    <head>
    <!-- set and print $page_title -->
    <title><?php echo $page_title='Su nny Day !'; ?></title>
    </head>
    <body>
    <?php
    // use saved $page_title
    echo 'The title of this page is (', $page_title, ')';
    ?>
    </body>
    </html>

    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Info 3000

      #3
      Re: Retrieve &lt;TITLE&gt ; in php code ?

      Thank you for this tip, but for the use that I want, it's necessary to
      retrieve the real TITLE... No way, is it your last word ? No function
      like get_title, or get_header_keyw ord("TITLE") or anything ?

      Only way is to write my own function, sure ?
      [color=blue][color=green]
      > ><HTML>
      > ><HEAD>
      > > <TITLE>Sunny day !</TITLE>
      > ></HEAD>
      > ><BODY>
      > > <?
      > > echo "The title of this page please (Sunny Day !)"
      > > ?>
      > ></BODY>
      > ></HTML>
      > >
      > > I think that you have understanding my problem : How to retrieve the
      > > title of my page ?[/color][/color]

      [color=blue]
      > I'd look at it differently:
      >
      > <html>
      > <head>
      > <!-- set and print $page_title -->
      > <title><?php echo $page_title='Su nny Day !'; ?></title>
      > </head>
      > <body>
      > <?php
      > // use saved $page_title
      > echo 'The title of this page is (', $page_title, ')';
      > ?>
      > </body>
      > </html>[/color]

      Comment

      • Doug Hutcheson

        #4
        Re: Retrieve &lt;TITLE&gt ; in php code ?

        "Info 3000" <troismille@hot mail.com> wrote in message
        news:54c588e5.0 402091358.35792 734@posting.goo gle.com...[color=blue]
        > Thank you for this tip, but for the use that I want, it's necessary to
        > retrieve the real TITLE... No way, is it your last word ? No function
        > like get_title, or get_header_keyw ord("TITLE") or anything ?
        >
        > Only way is to write my own function, sure ?
        >[color=green][color=darkred]
        > > ><HTML>
        > > ><HEAD>
        > > > <TITLE>Sunny day !</TITLE>
        > > ></HEAD>
        > > ><BODY>
        > > > <?
        > > > echo "The title of this page please (Sunny Day !)"
        > > > ?>
        > > ></BODY>
        > > ></HTML>
        > > >
        > > > I think that you have understanding my problem : How to retrieve the
        > > > title of my page ?[/color][/color]
        >
        >[color=green]
        > > I'd look at it differently:
        > >
        > > <html>
        > > <head>
        > > <!-- set and print $page_title -->
        > > <title><?php echo $page_title='Su nny Day !'; ?></title>
        > > </head>
        > > <body>
        > > <?php
        > > // use saved $page_title
        > > echo 'The title of this page is (', $page_title, ')';
        > > ?>
        > > </body>
        > > </html>[/color][/color]


        Try this:

        //Read the HTML file into an array:
        $HTML_file = file ('http://www.example.com/');

        //Convert this to a string separated by zero length strings:
        $HTML_string = implode('', $HTML_file);

        // then use a regexp to find what lies between the title tags:
        eregi("<title>( .*)</title>", $HTML_string, $my_temp);

        // $my_temp is now an array of matched patterns from the regexp:
        // the first (and only, in this case) parenthesised pattern is at array
        offset 1,
        // which is where we will find our title:
        $HTML_title = trim($my_temp[1]);

        HTH
        Doug
        --
        Remove the blots from my address to reply


        Comment

        • Matthias Esken

          #5
          Re: Retrieve &lt;TITLE&gt ; in php code ?

          troismille@hotm ail.com (Info 3000) schrieb:
          [color=blue]
          > Thank you for this tip, but for the use that I want, it's necessary to
          > retrieve the real TITLE... No way, is it your last word ? No function
          > like get_title, or get_header_keyw ord("TITLE") or anything ?[/color]

          Well, there might be a way. Somehow you have to get the HTML output in
          your program. Have a look at the output control functions at
          http://www.php.net/manual/en/ref.outcontrol.php.

          The idea is to catch the output in a buffer, search the buffer for the
          title and replace a placeholder in the HTML otput with the title. Have a
          look at the following code:

          <?php ob_start(); // start output buffering ?>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
          <title>Yeah, it works</title>
          </head>
          <body>
          The title of this page is: {TITLE}
          </body>
          </html>
          <?php
          // get the content of the buffer and clear the buffer
          $content = ob_get_clean();
          // find title
          if (preg_match("~< title>(.*)</title>~iU", $content, $match)) {
          // replace the placeholder {TITLE} with the real title
          $content = preg_replace("~ (\{TITLE\})~", $match[1], $content);
          }
          // output the result
          echo $content;
          ?>

          It worked for me. If you're using a PHP version < 4.3.0 you won't have
          the function ob_get_clean(). This function can be replaced by two other
          functions. Check the documentation if you need to.

          Regards,
          Matthias

          Comment

          • Geoff Berrow

            #6
            Re: Retrieve &lt;TITLE&gt ; in php code ?

            I noticed that Message-ID:
            <54c588e5.04020 91358.35792734@ posting.google. com> from Info 3000
            contained the following:
            [color=blue]
            >Thank you for this tip, but for the use that I want, it's necessary to
            >retrieve the real TITLE... No way, is it your last word ? No function
            >like get_title, or get_header_keyw ord("TITLE") or anything ?
            >
            >Only way is to write my own function, sure ?[/color]

            How is the page generated?

            --
            Geoff Berrow (put thecat out to email)
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

            Comment

            Working...