...

View Full Version : Get title of page



buildrebuild
02-04-2007, 04: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";

Thanks
CB

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

chump2877
02-04-2007, 06: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...

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



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

chump2877
02-04-2007, 09: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:

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

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

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

true, it's possible:

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum