【发布时间】:2015-07-31 21:42:01
【问题描述】:
我正在尝试在 HTML 标记中获取“lang”属性的值(使用 cURL 获取,一切顺利)。超级清理的 HTML 如下所示:
<html lang="en">
<head>
<title>Example</title>
</head>
<body></body>
</html>
当我使用时:
// Get HTML tag
$html = $xpath->query('//html');
echo '<pre>'. print_r($html, true) .'</pre>';
// Does a HTML tag exist at all?
if($html->length == 0) {
$htmlUsed = false;
}
// If HTML tag exists get value
if($html->length > 0) {
foreach($html as $tag) {
echo '<pre>'. print_r($tag->attributes, true) .'</pre>';
foreach($tag->attributes as $attribute) {
echo $attribute;
}
}
}
打印出来:
DOMNodeList Object
(
[length] => 1
)
DOMNamedNodeMap Object
(
[length] => 0
)
如何在 HTML 元素中获取此属性的值?它存在于获取页面(我在其上执行 XPath 查询)的 cURL 的 $response 中。注意:$tag->getAttribute('lang') 不会返回想要的结果,因为 $tag->attributes 看起来是空的。
【问题讨论】:
-
$html->item(0)->attributes->item(0)->nodeValue
标签: php html curl xpath attributes