Page Title

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

    #1

    Page Title

    Is it possible to return the Page title in php ?

    I can return the end of the url with $_SERVER["PHP_SELF"] however it
    is the "Page Title" that I require.

    Thanks
  • Heiko Richler

    #2
    Re: Page Title

    Stuartsblog wrote:
    Is it possible to return the Page title in php ?
    >
    I can return the end of the url with $_SERVER["PHP_SELF"] however it
    is the "Page Title" that I require.
    Not really. The Title is part of the response your script generates.
    PHP_SELF is a value given by your Webserver and more representing the
    request that cases your skript to run.

    Heiko

    Comment

    • Rik Wasmus

      #3
      Re: Page Title

      On Thu, 15 May 2008 20:53:55 +0200, Stuartsblog
      <stuart@stuarts tuart.fsnet.co. ukwrote:
      Is it possible to return the Page title in php ?
      >
      I can return the end of the url with $_SERVER["PHP_SELF"] however it
      is the "Page Title" that I require.
      Not just like that. To PHP, all previous output is just a random string
      for all it's concerned. If the page title of the current page is of
      importance, you should know it earlier on, or at least save it. If you're
      in the habit of just maxing PHP & HTML, teach yourself to prepare all data
      in strings before any output is generated, and just ending it with a
      outputting HTML and the previous data stored.

      An illustration, if your page is kind of like this:
      <html>
      <head><title><? php echo some_function_o utput(); ?></title>
      <body>
      <?php
      mysql_connect(. ..);
      $r =mysql_query('S ELECT data FROM table');
      $data = mysql_fetch_ass oc($r)
      echo $data['data'];
      //euhm, title change?
      ?>
      </body>
      </html>

      Change it to this:

      <?php
      $title = some_functions_ output();
      mysql_connect(. ..);
      $r =mysql_query('S ELECT data FROM table');
      $data = mysql_fetch_ass oc($r)
      $content = $data['data'];
      //title change
      $title .= 'output succeeded';
      ?>
      <html>
      <head><title><? php echo $title; ?></title>
      <body>
      <?php echo $content; ?>
      </body>
      </html>


      If that cannot be done, or you need a quick fix, here is some majorly
      dirty code I'd slap a previous coder for using:

      <?php
      ob_start();
      function get_title(){
      $send_so_far = ob_get_contents ();
      if(preg_match(' @<title[^>]*>(.*?)</title>@si',$sen d_so_far,$match )){
      return $match[1];
      }
      return false;
      }
      ?>
      <html>
      <head><title>fo obar</title>
      <body>
      <?php echo get_title(); ?>
      </body>
      </html>

      --
      Rik Wasmus
      [SPAM] Now temporarily looking for some smaller PHP/MySQL projects/work to
      fund a self developed bigger project, mail me at rik at rwasmus.nl. [/SPAM]

      Comment

      Working...