Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Get title of page

    I have this variable which is printed out on page load.
    Can I retreive the page title so it can be inserted into a breadcrumb?

    $titleofpage = "This is my page";

    Thanks
    CB

  • #2
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Can't you just put $titleofpage into your breadcrumb? Else you will have to show us some code.

  • #3
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,773
    Thanks
    19
    Thanked 155 Times in 146 Posts
    You want what's between the <title> tags in your file?

    You might be able to access the page source with file_get_contents() and then use some regex to get the title tag contents, or you could use the DOM to extract the content of the title tag node...
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,469
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    This is how to read that title from any webpage:

    PHP Code:
    <?php
    // Read website
    $url "http://www.amazon.com";
    $data implode(""file($url)); 
    // Get <title> line
    preg_match ("/<title>([^`]*?)<\/title>/"$data$match);
    $titleofpage $match[1];

    echo 
    $titleofpage;
    ?>

  • #5
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,773
    Thanks
    19
    Thanked 155 Times in 146 Posts
    don;t forget the "i" modifier in your regex for those who still code in HTML 4 instead of XHTML, and I don;t see why you would prohibit a match if there was a single quote in the title:

    "/<title>(.*?)<\/title>/i"

    and I think file_get_contents() is the preferred method according to the PHP manual, but don;t ask me why becuase I don;t remember
    Last edited by chump2877; 02-04-2007 at 09:50 PM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #6
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Also, <title> can have attributes.

  • #7
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,773
    Thanks
    19
    Thanked 155 Times in 146 Posts
    Also, <title> can have attributes.
    true, it's possible:

    preg_match ("/(<title)(.*?)(>)(.*?)(<\/title>)/i", $data, $match);
    $titleofpage = $match[4];
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •