Get Webpage Title and Meta Description from URL in PHP

On 6/14/2016

Hi! Today we'll see how to get webpage title and description from url in php. At times you may want to scrape some url and retrieve the webpage contents. You may need some third-party DOM Parser for this sort of task but PHP is uber-smart and provides you with some fast native solutions. Retrieving page title and meta description tags from url is lot easier than you think. Here we'll see how to do it.

A Web Page title can be found between the <title> and </title> tag and description within <meta> tag. Page title is self-explanatory and meta tags store various useful information about a webpage like title, description, keywords, author etc.

php-get-web-page-title-description-from-url

PHP Code to Get Webpage Title from URL:

In order to get/retrieve web page title from url, you have to use function file_get_contents() and regular expression together. Here is the php function to retrieve the contents found between <title> </title> tag.

<?php
// function to get webpage title
function getTitle($url) {
    $page = file_get_contents($url);
    $title = preg_match('/<title[^>]*>(.*?)<\/title>/ims', $page, $match) ? $match[1] : null;
    return $title;
}
// get web page title
echo 'Title: ' . getTitle('http://www.w3schools.com/php/');

// Output:
// Title: PHP 5 Tutorial
?>

PHP Code to Get Webpage Meta Description from URL:

Get/Retrieve meta description from webpage is even more easier with php's native get_meta_tags() method. The function get_meta_tags() extracts all the meta tag content attributes from any file/url and returns it as an array.

Here is the php function to get the meta description from url.

<?php
// function to get meta description
function getDescription($url) {
    $tags = get_meta_tags($url);
    return @($tags['description'] ? $tags['description'] : "NULL");
}
// get web page meta description
echo 'Meta Description: ' . getDescription('http://www.w3schools.com/php/');

// Output:
// Meta Description: Well organized and easy to understand Web bulding tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, and XML. 
?>

Some WebPages may miss meta tags and the above function will return null value if it is not present.

Also you can retrieve the rest of the meta tags in the same way.

<?php
echo $tags['keywords'];
echo $tags['author'];
...
?>

We have seen how to get webpage title and meta description from url in php. I hope you enjoy this tutorial. If you have any queries please let me know through your comments.

2 comments:

  1. PHP provides the best option to build the website where we can design our website in a very interactive manner that provides better functioning in data management.
    PHP Training in Chennai | PHP course in Chennai

    ReplyDelete
  2. PHP provides the best option to build the website where we can design our website in a very interactive manner that provides better functioning in data management.
    PHP Training in Chennai | PHP course in Chennai

    ReplyDelete

Contact Form

Name

Email *

Message *