【发布时间】:2011-08-19 21:32:52
【问题描述】:
标签: php meta-tags robots.txt
标签: php meta-tags robots.txt
function get_meta($url)
{
// Get & Tidy HTML
$tidy = new tidy();
$tidy->parseFile($url, array("output-html" => true));
$tidy->cleanRepair();
// Parse XML
$xml = new DOMDocument();
$xml->loadHTML($tidy);
$meta_tags = $xml->getElementsByTagName("meta");
// Put meta informations in an array
$meta = array();
foreach($meta_tags as $meta_tag)
{
$key = $meta_tag->hasAttribute("http-equiv") ? $meta_tag->getAttribute("http-equiv") : $meta_tag->getAttribute("name");
$value = $meta_tag->hasAttribute("content") ? $meta_tag->getAttribute("content") : $meta_tag->getAttribute("value");
$meta[$key] = $value;
}
return $meta;
}
print_r(get_meta("http://php.net/manual/fr/tidy.cleanrepair.php"));
【讨论】:
你可以:
使用 file_get_contents 检索原始 HTML 数据
Tidy HTML 代码使其更具可读性;如果您的 Web 服务器上没有安装 Tidy:
apt-get install php5-tidy
使用 DOMDocument
【讨论】: