2006 Sep 21

Getting website title and description

Getting the a website title and description is easy. Using the PHP's builtin file_get_contents command together with a regex pattern allows us to capture and get any website title and description without any complex methods that is if the site has a title or a description. In case a site has no description a simple excerpt function is also provided.

Getting the site title:

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

The code above returns the title of the site enclosed by the tags <title> and </title>. The function would return a boolean false in case there was none.

Getting the meta description:

function getMetaDescription($content) {
$metaDescription = false;
$metaDescriptionPatterns = array("/]*>/Ui", "/]*>/Ui");
foreach ($metaDescriptionPatterns as $pattern) {
if (preg_match($pattern, $content, $match))
$metaDescription = $match[1];
break;
}
return $metaDescription;
}

The code above returns the meta description of the site enclosed with single quotes or double quotes. It will return a boolean false it there wasn't any. If this would happen we could get an excerpt of maybe the first website sentence to serve as our website description instead, however getting an excerpt would not be very efficient and i had some trouble with my code. Please fell free to make a comment to optimize it.

Getting the first website sentence:

function getExcerpt($content) {
$text = html_entity_decode($content);
$excerpt = array();
//match all tags
preg_match_all("|<[^>]+>(.*)]+>|", $text, $p, PREG_PATTERN_ORDER);
for ($x = 0; $x < sizeof($p[0]); $x++) {
if (preg_match('< p >i', $p[0][$x])) {
$strip = strip_tags($p[0][$x]);
if (preg_match("/\./", $strip))
$excerpt[] = $strip;
}
if (isset($excerpt[0])){
preg_match("/([^.]+.)/", $strip,$matches);
return $matches[1];
}
}
return false;
}

The code above reads the entire page and looks for the <p> tag, then returns the first phrase that ends with a period and stripping all the html code inside.

Here's a sample code to test our script:

$url = 'http://www.tildemark.com/';
$content = file_get_contents($url);
$title = getMetaTitle($content);
$description = getMetaDescription($content);
$excerpt = getExcerpt($content);
print "title: $title ";
print "< br />";
print "description: $description ";
print "< br />";
print "excerpt: $excerpt";
?>

You may download a working copy of the title and description scraper script.

Thank you for the comment:
Yes, indeed. We could use the builtin get_meta_tags function to get the website description without any knowledge on regular expressions. here's how:

<?php $meta_data= get_meta_tags('http://www.tildemark.com/'); echo $meta_data['description']; ?>

Aside from getting the description, you could also get Author, Keyword and GeoPosition meta data using the function get_meta_data().

0 TrackBacks

Listed below are links to blogs that reference this entry: Getting website title and description.

TrackBack URL for this entry: http://www.tildemark.com/cgi-bin/mt4/mt-tb.cgi/29

3 Comments

you did everything the codes are wonderful, but then it's hard to understand from the new comers of regular expression... so better to use a built-in PHP function of extracting meta tag contents... this is how to extract contents under description tag...

simply use "get_meta_tags"



I'm not sure if the get_meta_tags() function uses the cURL extension in fetching the contents of the website whose meta data is to be parsed. If so, then this function is just too handy that I would place this into my list of favorite PHP functions.

On the otherhand, if this (get_meta_tags()) function is not using cURL, maybe it is worth to note the benchmark details of this function. In this way, we could reconsider writing a function that will do the same that uses cURL to fetch the contents of the page to be parsed as what the author of this blog is doing.

yeah. get_meta_tags is a bit slower. But, on the brighter side its much easier.

Leave a comment

About this Entry

This page contains a single entry by tildemark published on September 21, 2006 7:04 PM.

What color is an octopus? was the previous entry in this blog.

Google hints is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Recent Activity

Friday

  • tildemark tweeted, "im so sleepy. Zzzzzzzz"

Sunday

  • tildemark tweeted, "some of my scipts are not working with godaddy. but works fine on the others. not mention their poorly coded admin page"

Today

  • tildemark tweeted, "so many pending tasks i need to finish. need more coffee !!!"
  • tildemark tweeted, "@gmtristan i dont think that is true."

Today

  • tildemark tweeted, "how does godaddy subdomain behaves? i have some problems with it on my scripts. it does not seem to accept query strings.."

Monday

  • tildemark tweeted, "i had a hard time removing the error messages generated by surf side kick. i ended up uninstalling most of my applications."

Sunday

  • tildemark tweeted, "i got hit by surf side kick and im getting numerous error messages on my screen. tskkkkk"

Saturday

  • tildemark tweeted, "check boxes, i didn't know they can also be complex"
  • tildemark tweeted, "this smart bro internet speed is depressing, i thinking of filling a complaint to the DTI next week."

Friday

  • tildemark tweeted, "the seminar turned out to be leadership training. it was fun, learned alot. i have already attended numerous seminars but this is different."