html - PHP to get meta tags after <head> tag -
i want meta tags external url. unfortunately, meta tags on website placed after tag. use get_meta_tags($url)
didn't work. here external url source , meta tag description exist @ last.
<html><meta http-equiv="content-type" content="text/html; charset=utf-8"> <head><title>tools</title> </head> <body><h2>sitemap notification received</h2> <br> sitemap has been added our list of sitemaps crawl. if first time notifying google sitemap, please add via <a href="http://www.google.com/webmasters/tools/">http://www.google.com/webmasters/tools/</a> can track status. please note not add submitted urls our index, , cannot make predictions or guarantees when or if appear.</body></html> <meta name='description' content='200'>
this function should you:
function file_get_contents_curl($url) { $ch = curl_init(); curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_followlocation, 1); $data = curl_exec($ch); curl_close($ch); return $data; } $html = file_get_contents_curl("http://example.com/"); //parsing begins here: $doc = new domdocument(); @$doc->loadhtml($html); $nodes = $doc->getelementsbytagname('title'); //get , display need: $title = $nodes->item(0)->nodevalue; $metas = $doc->getelementsbytagname('meta'); ($i = 0; $i < $metas->length; $i++) { $meta = $metas->item($i); if($meta->getattribute('name') == 'description') $description = $meta->getattribute('content'); if($meta->getattribute('name') == 'keywords') $keywords = $meta->getattribute('content'); } echo "title: $title". '<br/><br/>'; echo "description: $description". '<br/><br/>'; echo "keywords: $keywords";
or easier this:
<?php // assuming above tags @ www.example.com $tags = get_meta_tags('http://www.example.com/'); // notice how keys lowercase now, , // how . replaced _ in key. echo $tags['author']; // name echo $tags['keywords']; // php documentation echo $tags['description']; // php manual echo $tags['geo_position']; // 49.33;-86.59 ?>
Comments
Post a Comment