PHP Tutorial – Retrieve WebPage Titles and Meta Tags

In case you do not know: a web page title is found between the <title> and </title> HTML tag while that of meta description is in <meta>.


Let take this web-page url as an example

It has a page title – Giveaway #1 – Win Free e-Copy of Jump Start PHP Book with HTML title tag <title>Giveaway #1 - Win Free e-Copy Of Jump Start PHP Book</title>.

Meta Description – <meta name="description" content="Win a free e-book copy of sitepoint&#039;s Jump Start PHP Book"/>

More information on Meta tag here.

Programmatically retrieving a web page title and meta tags is made possible by regex and PHP get_meta_tags function.

  • Retrieving Web Page title

    Use the below PHP function powered by regex to retrieve the title of an HTML document / web-page.

    	function getTitle($url) {
    		$data = file_get_contents($url);
        $title = preg_match('/<title[^>]*>(.*?)<\/title>/ims', $data, $matches) ? $matches[1] : null;
        return $title;

    To use this function, use the web-page URL as the getTitle() argument.
    For example, the code below will retrieve and output the title of

    echo getTitle('');
  • Retrieving Web Page Meta-tags

    PHP get_meta_tags function extracts all meta tag content attributes from a file / URL parsed as the function argument and returns an array.
    Let’s see a code example of how this function works. Take cognizance of comments.

    // Assuming the above tags are at 
    $tags = get_meta_tags('');
    // Note: the array keys are the respective meta tag name attribute 
    // and are all lowercase 
    echo $tags['description']; // Win a free e-book copy of sitepoint's Jump Start PHP Book
    echo $tags['keywords'];     // Book Review,Giveaways,Download Jump Start PHP,freebies,giveaway
    echo $tags['twitter:creator'];       // @tech4sky
    echo $tags['generator'];  // WordPress 3.8

    I have shown us how to programmatically retrieve a webpage’s title and Meta tags using PHP.
    Automagic retrieval of website titles and meta-tags are famous feature found in social bookmarking site like reddit, BlogEngae, etc.

About Agbonghama Collins

Agbonghama Collins is a webdeveloper and an internet geek. I love writing and sharing the little idea & information i know. Hope you enjoyed my Article.

Follow me on Twitter, add him on Facebook or on Google+