Hi , I would like to fetch the meta decription and keyword of many sites .I have a script to do it.While doing so I have come across three different meta tag structure : 1. <meta xmlns="http://wwww.w3.org/1999/xhtml" name="description" content="meta decription goes here" /> 2.<meta name="description" content="meta decription goes here" /> 3.<meta http-equiv="description" content="meta decription goes here" /> How to fetch the description.Due to this difference in structure i am unable to fetch the details and as well us i need to know is there any other type other than the above three. FYI:Same goes to meta keywords. Have anybody faced this problem? Do you have any solution or suggestion. Please advice me. Cheers.
Here is the PHP code i made for you: <?php $site=file_get_contents("http://aioshared.com"); $head=explode("<head>",$site); $head=explode("</head>",$head[1]); $head=$head[0]; $desc=explode('name="description"',$head); $desc=explode("content=",$desc[1]); $desc=explode(">",$desc[1]); $arr=array('"','/'); $description=str_ireplace($arr,"",$desc[0]); if ($description==""){ $description="Can't Fetch Description"; } $keyw=explode('name="keywords"',$head); $keyw=explode("content=",$keyw[1]); $keyw=explode(">",$keyw[1]); $arr=array('"','/'); $keywords=str_ireplace($arr,"",$keyw[0]); if ($keywords==""){ $keywords="Can't Fetch Keywords"; } echo "<b>Description:</b>".$description."<br>"; echo "<b>Keywords:</b>".$keywords."<br>"; ?> PHP: If you face any problem with it, then let me know Thanks!
#3 is just bad html and I don't think you'll come across it too often. For the rest, this should work: $dom = new DOMDocument(); @$dom->loadHTML($html); $metas = $dom -> getElementsByTagName('meta'); foreach ($metas as $meta) { if ( $meta ->getAttribute('name') == 'description') { echo $meta->getAttribute('content'); } } PHP: Where $html is a string with your html data.
How about the following: // Assuming the above tags are at www.example.com $tags = get_meta_tags('http://www.example.com/'); // Notice how the keys are all lowercase now, and // how . was replaced by _ in the key. echo $tags['author']; // name echo $tags['keywords']; // php documentation echo $tags['description']; // a php manual echo $tags['geo_position']; // 49.33;-86.59 PHP: