通过SimpleXML从网页中抽取出网页标题和描述,页面内容被传递给simplexml_load_string()函数后被解析成一个XML文档并返回一个容易处理的对象。如果系统中安装了libtidy扩展,可使用它来规范代码,否则就消除可能产生的警告信息:
$file = tidy_repair_string($file);
$html = simplexml_load_string($file);
// or: $html = @simplexml_load_string($file);
// extact the title
if ($html->head->title)
{
$title = $html->head->title;
}
else
{
// use the filename if a title is not found
$title = basename($row['DOCUMENT_URL']);
}
// extract the description
$description = ‘No description provided.’;
foreach($html->head->meta as $meta)
{
if (isset($meta['name']) && $meta['name'] == ‘description’)
{
$description = $meta['content'];
break;
}
}