Getting the page title

Discussion in 'PHP' started by enchance, May 8, 2008.

  1. #1
    Is there a php command which gets the current page's page title? Or even better can it go through the history and grab a selected page's title?
    <title>How do I get this line of text into my php code?</title>
    HTML:

     
    enchance, May 8, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Since php is parsed before html code, there is no way to get the title of the page php is executing from. You could set the title in php and echo it out in the actual title tag, but you can't get the title of the page that the code is being executed on.

    If you need to get the title of another page, you could use a function like file_get_contents and use a regular expression to extract the title from the page. Not sure if that helps any.
     
    jestep, May 8, 2008 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    You can store the HTML output in a variable instead of printing it, which you can play with it later on (like templates).

    Peace,
     
    Barti1987, May 8, 2008 IP
  4. tamilsoft

    tamilsoft Banned

    Messages:
    1,155
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #4
    But, you can get using javascript:

    <script language="javascript">
    alert(document.title);
    </script>
    Code (markup):
     
    tamilsoft, May 9, 2008 IP
  5. Estevan

    Estevan Peon

    Messages:
    120
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    0
    #5
    hello

    use this
    // USING FILE GET CONTENTS
    $_pagecontent=file_get_contents("http://www.siteurl.com");

    preg_match("/<title>(.*)<\/title>/smU",$_pagecontent, $results);
    $title=strip_tags($results[0]); // here is the page title
     
    Estevan, May 9, 2008 IP