Skip to main content

Get all the meta tags of the page

This function gives a well formatted array of meta tags of a page. It can take care of meta tags such as keywords,description,all robots,googlebot etc.
<?php
 function getMetaTags($content)
 {
  $return_array=array();
  $meta_tags=extractAllMetaTags($content);
  
  //get all keywords meta tags
  $pattern_kw="/\sname\s*=\s*[\"\']*keywords[\"\']*/ims";
  $pattern_content="/\scontent\s*=\s*[\"\']?\s*([^\"\']+)[\"\']?(.*?)>/ims";
  $return_array['keywords']=array();
  foreach($meta_tags[0] as $tag_key=>$tag)
  {
   if(preg_match($pattern_kw,$tag))
   {
    if(preg_match($pattern_content,$tag,$matches_content))
    {
     array_push($return_array['keywords'],$matches_content[1]);
    }
   }
  }
  
  //get all description meta tags
  $matches_content=array();
  $pattern_description="/\sname\s*=\s*[\"\']*description[\"\']*/ims";
  $return_array['description']=array();
  foreach($meta_tags[0] as $tag_key=>$tag)
  {
   if(preg_match($pattern_description,$tag))
   {
    if(preg_match($pattern_content,$tag,$matches_content))
    {
     array_push($return_array['description'],$matches_content[1]);
    }
   }
  }
  
  //get all robots meta tags
  $matches_content=array();
  $pattern_robots="/\sname\s*=\s*[\"\']*robots[\"\']*/ims";
  $return_array['robots']=array();
  foreach($meta_tags[0] as $tag_key=>$tag)
  {
   if(preg_match($pattern_robots,$tag))
   {
    if(preg_match($pattern_content,$tag,$matches_content))
    {
     array_push($return_array['robots'],$matches_content[1]);
    }
   }
  }
  
  //get all googlebot meta tags
  $matches_content=array();
  $pattern_googlebot="/\sname\s*=\s*[\"\']*googlebot[\"\']*/ims";
  $return_array['googlebot']=array();
  foreach($meta_tags[0] as $tag_key=>$tag)
  {
   if(preg_match($pattern_googlebot,$tag))
   {
    if(preg_match($pattern_content,$tag,$matches_content))
    {
     array_push($return_array['googlebot'],$matches_content[1]);
    }
   }
  }
  
  //get all meta refreshes 
  $matches_content=array();
  $pattern_refresh="/\shttp-equiv\s*=\s*[\"\']*refresh[\"\']*/ims";
  $return_array['refresh']=array();
  foreach($meta_tags[0] as $tag_key=>$tag)
  {
   if(preg_match($pattern_refresh,$tag))
   {
    if(preg_match($pattern_content,$tag,$matches_content))
    {
     array_push($return_array['refresh'],$matches_content[1]);
    }
   }
  }
  
  return $return_array;
  
  
 }

 function extractAllMetaTags($content)
 {
  $pattern="/<meta(.*?)>/ims";
  preg_match_all($pattern,$content,$matches);
  return $matches;  
 }


?>

Comments

  1. Please copy the extractAllMetaTags function from the previous post. Post has title Get all meta tags of a page

    ReplyDelete

Post a Comment

Popular posts from this blog

Difference between SJAX and AJAX

AJAX is Asynchronous Javascript and XML SJAX is Synchronous Javascript and XML What does Asynchronous means ? It means that when a request is sent to the server the statements following (javascript code OTHER than the callback function ) are executed WITHOUT waiting for the server response. So even though if your server is taking a long time for responsing for a request , you are not put on a hold. That's the beauty of AJAX. Synchronous means here exactly the opposite of it. That means you force the browser to wait for the server response. This might be a bit a dangerous step because if the server takes a long time,the browser may appear dead. Your users might have to close the tab or the browser. How to send Synchronous request by jQuery? $.ajax method of jquery supports a lot of options , there is an option called async which is true by default . You can set it to false. You can find it here

Ajax with Jquery

This time I wanted to write something for Jquery. Jquery is a very good Javascript library. It reduces much of your coding time. So here is how you do AJAX with Jquery Step 1 -: Download the latest version of jquery from here. This is Jquery 1.4 minified version. Step 2 -: $.get(url,'',function(data){ alert("Data from ajax is "+data); }); Done !!! Now I will explain it- 1.The "$" is nothing but a shorthand notation for jQuery. 2.Then comes "get" , this can be replaced with post as well, as this is the method by which the http request will be sent to the server. 3.Next is "url". This is the url of the page you are requesting. 4.After url I have the next parameter is left as '' . This parameter is just for adding the parameters (query strings) to the url. It is left to the programmer that he wants to use this parameter or not . You can club your query string in the Url also. 5.Finally we have a ...

Anchor Text - Extract All of them

This function finds all the href links of a page including their anchor text as well. This function is also capable of finding href links which either have an images or html tags in the text part. I have seen a lot of regular expressions in books and internet ,but they miss out those href links which have images or html tags in them. This gives an array containing the links and their anchor texts . <?php /* $content is an argument which contains the contents of html page in a string. */ function getURLsFromPage($content) { $images=getImageTags($content); foreach($images as $image) { $content=str_replace($image,"",$content); } $anchors=getAnchorText($content); foreach($anchors as $anchor) { $content=str_replace($anchor,strip_tags($anchor),$content); } //get all urls in a page irrespective of their domains... $matches = array(); URL_pattern = "/\s+href\s*=\s*[\"\']?\s*([^\s\"\']+)[\"\'\s]?\s*[^>]*>([^<]*)<\/a>/ims...