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; } ?>
Please copy the extractAllMetaTags function from the previous post. Post has title Get all meta tags of a page
ReplyDelete