How To Capture a Page Title Using PHP

Since we know that the title of the page in html will be inside the title tag, we can use the following regular expression to capture the page title of a page easily.


$title_regex = '%<title>(.+)<\/title>%i';
$page = file_get_contents(urlencode("http://www.google.com"));
$matches = array();
 if (preg_match($title_regex, $page, $matches) && isset($matches[1]))
$title = $matches[1];
else
$title = "Not Found";
echo $title;

One thought on “How To Capture a Page Title Using PHP

Leave a comment