我已经在某些HTML中加载了这个PHP代码。
$dom = new DOMDocument();
$dom->loadHTML($somehtml);
$xpath = new DOMXPath($dom);
$divContent = $xpath->query('//table[class="defURLP"]');
echo $divContent;
我太难以理解需要在这里发生什么,但是我希望能够填充变量$ divContent以使用类名defURLP
来获取表的html内容目前正在返回
object(DOMNodeList)#3 (0) { }
答案 0 :(得分:1)
您需要从xpath查询返回的DOMNodeList中检索第一个项目,因为列表中可能有多个项目。
// Queries for tables having class defURLP
$tables = $xpath->query('//table[class="defURLP"]');
// Reference the first one in $divContent
$divContent = $tables->item(0);
// Output its nodeValue
echo $divContent->nodeValue;
或者使用foreach
:
$tables = $xpath->query('//table[class="defURLP"]');
// Iterate over the whole node list in $tables (if it is multiple nodes)
foreach ($tables as $t) {
echo $t->nodeValue;
}