使用Zend_Dom_Query,我想从一个超文本标记语言字符串中检索元数据。
要检索链接,您可以像这样简单地查询:
$results = $dom->query('a'); //Where $dom is your html string不幸的是,这似乎不适用于元数据
$results = $dom->query('meta'); //doesn't work如何检索元数据,然后按其'property‘属性进行过滤?
下面是我正在寻找的一个示例:
public function meta($dom)
{
$results = $dom->query('meta'); //This is not a correct query (does anyone have an alternative?)
$links = array();
foreach ($results as $index => $result) {
if ($result->getAttribute('property') == 'title') { //find <meta property="title"
echo $result->getAttribute('content') . '<br />'; //echo the content attribute of the title
}
}
return $results;
}一旦查询正确,这段代码就可以工作了。然而,我想更进一步,直接查询<meta property="title" content="This is the Title" />,而不是检索所有的元并循环获取正确的元。
使用zend_dom_query获取所有元数据的任何帮助,或者(更重要的)查询以仅接收元数据,其中属性==标题将受到重视。
谢谢
发布于 2012-09-27 22:26:53
CSS元标记不是有效的选择器,因此您必须使用$dom->queryXpath($xPathQuery)方法而不是$dom->query()。
可能是这样的:
$dom->queryXpath('/html/head');我不确定要使用的确切查询字符串,但这就是我的想法。
Zend_Dom_Query Theory of Operation.
发布于 2013-06-13 11:50:10
如果你有url,试试这个:
$metatagarray = get_meta_tags($url);
if (!empty($metatagarray“关键字”))
$metakey =$metatagarray“关键字”;
if (!empty($metatagarray"description"))
$metadesc =$metatagarray“描述”;
https://stackoverflow.com/questions/12621806
复制相似问题