可能重复:
帮助获取元标题和描述
我花了一整天的时间在网上搜索。在satckoverflow上也看到了一些类似的问题。但我都失望了。
我想获得一些php代码,通过这些代码我可以输出标题和一些4-5行,用于描述任何使用php的网站。
<?php
$url = "http://www.drquincy.com/";
$fp = fopen($url, 'r');
$content = "";
while(!feof($fp)) {
$buffer = trim(fgets($fp, 4096));
$content .= $buffer;
}
$start = '<title>';
$end = '</title>';
preg_match(" / $start( . * )$end / s", $content, $match);
$title = $match[1];
$metatagarray = get_meta_tags($url);
$keywords = $metatagarray["keywords"];
$description = $metatagarray["description"];
echo " <div><strong>URL: </strong >$url</div> 'n";
echo " <div><strong>Title: </strong >$title</div> 'n";
echo " <div><strong>Description: </strong >$description</div>'n";
echo " <div><strong>Keywords: </strong >$keywords</div>'n";
只需更改url:(
解析HTML有很多方法。首先,你想要内容本身:
$res = file_get_contents("http://www.google.com");
这假定允许file_get_contents访问uri。例如,您可以使用regex:
preg_match("~<title>(.*?)</title>~", $res, $match);
$title = $match[1];
但是最好使用DOM解析器。http://php.net/manual/en/book.xml.php,但如果目标内容不是有效的xml,则可能会出现问题。