Saturday 13 February 2016

How to get meta keywords and description of a web page?

get_meta_tags() function return all meta tags of web page.
For example:
$url = "http://www.example.com";   
$tags = get_meta_tags($url);
$description     = $tags["description"];
$keywords         = $tags["keywords"];
echo 'Keywords: '.$keywords;
echo '<br/>';
echo 'Description: '.$description;

How to get title of website via website url?

The Title of website can get via site url by using file_get_contents() and php Simple html DOM parser.
php Simple html DOM parser is useful for php deveopers because php developers can easily find DOM elements of web page using php.

Getting title with file_get_contents()
<?php
function get_title($url){
  $str = file_get_contents($url);
  if(strlen($str)>0){
    $str = trim(preg_replace('/\s+/', ' ', $str));
    preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title);
    return $title[1];
  }
}
//For Example:
echo get_title("http://www.example.com/");
?>

Getting title with php Simple html DOM parser
// Include the library
include('simple_html_dom.php');
/ Retrieve the DOM from a given URL
$html = file_get_html('http://www.example.com/');
// Find title tag
$titleTag        = $html->find('title',0)->innertext;
echo 'Ttile: '.$titleTag;