我得到了上面的错误,并试图打印出对象,看看我如何访问它内部的数据,但它只回显DOMNodeList对象()
function dom() {
$url = "http://website.com/demo/try.html";
$contents = wp_remote_fopen($url);
$dom = new DOMDocument();
@$dom->loadHTML($contents);
$xpath = new DOMXPath($dom);
$result = $xpath->evaluate('/html/body/table[0]');
print_r($result);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Wordpress,因此解释了wp_remote_fopen函数.我试图从$ url回显第一个表
ont*_*ia_ 15
是的,DOMXpath::query回报总是一个DOMNodeList,这是一个奇怪的对象来处理.你基本上必须迭代它,或者只是item()用来获得一个项目:
// There's actually something in the list
if($result->length > 0) {
$node = $result->item(0);
echo "{$node->nodeName} - {$node->nodeValue}";
}
else {
// empty result set
}
Run Code Online (Sandbox Code Playgroud)
或者你可以遍历值:
foreach($result as $node) {
echo "{$node->nodeName} - {$node->nodeValue}";
// or you can just print out the the XML:
// $dom->saveXML($node);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17802 次 |
| 最近记录: |