$doc = new DOMDocument();
$doc->loadHTML($htmlcode);
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//*[@id='detail']/div[1]/h3/text()");
return var_dump($elements);
我如何正确检索xpath?它正在回归
object(DOMNodeList)#4 (1) {
["length"]=>
int(1)
}
当我做一个var_dump($ doc);它包含html为[“textContent”] =>串(22304)
我使用谷歌浏览器复制了xpath
答案 0 :(得分:1)
无法像您期望的那样使用DOMNodeList
内省var_dump()
。您必须访问列表项0
及其节点值:
$elements = $xpath->query("//*[@id='detail']/div[1]/h3/text()");
if($elements->length === 1) {
echo $elements->item(0)->nodeValue;
} else {
...
}