PHP XPath 检查是否为空

Mar*_*eld 2 php xpath dom

我有这个 PHP 代码:

$document = new DOMDocument();
$document->loadHTML( $html );

$xpath = new DomXPath($document);

$tables = $xpath->query("//*[contains(@class, 'info')]");
$tableDom = new DomDocument();  
$tableDom->appendChild($tableDom->importNode($tables->item(0), true));
Run Code Online (Sandbox Code Playgroud)

如何检查 $tables 变量是否包含我们可以在 $tableDom 中使用的内容?

我试过:

if (!empty($tables)) {
    echo("</br>not empty</br>");
} else {
    echo("empty");
}

if (!$tablese) {
    echo("empty</br>");
}
Run Code Online (Sandbox Code Playgroud)

但是它总是说它不是空的,所有的 HTML 都不包含带有类信息的表格。

Nag*_*aga 5

像这样尝试

if ($tables->length>0) {
    echo("</br>not empty</br>");
} else {
    echo("empty");
}
Run Code Online (Sandbox Code Playgroud)