Monday, August 2, 2010

Get the Page title of a webpage using PHP

Below is the function for getting the Title of the webpage using PHP

function getMetaTitle($content){
$pattern = "|<[\s]*title[\s]*>([^<]+)<[\s]*/[\s]*title[\s]*>|Ui";
if(preg_match($pattern, $content, $match))
return $match[1];
else
return false;
}

$url = $siteurl; //Enter the Site URL which we want to get the Page Title
$content = file_get_contents($url);
$title = getMetaTitle($content);
$tags = get_meta_tags($siteurl); // To get the meta content, description, keywords of the given URL....


$tags is a array of all meta data
To get the Meta Keywords, Description we will use the get_meta_tags which is a predefined function in PHP. To retrieve the values we need to use $tags['keywords'] for Keywords & for Description $tag['description']


 

No comments: