View Full Version : Get title of page

02-04-2007, 05:16 PM
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";


02-04-2007, 05:38 PM
Can't you just put $titleofpage into your breadcrumb? Else you will have to show us some code.

02-04-2007, 07:55 PM
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...

02-04-2007, 10:38 PM
This is how to read that title from any webpage:

// 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;

02-04-2007, 10:47 PM
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:


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 :)

02-04-2007, 10:54 PM
Also, <title> can have attributes.

02-04-2007, 10:57 PM
Also, <title> can have attributes.

true, it's possible:

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