从 HTML 获取 domXPath 的值

get the value of domXPath from HTML

我需要从我的 html 中解析 <span id="the_name"> 标签的内容。
html 看起来像这样:

...
<span id="userName" class="username"></span> 
<div class="main"> 
 <div class="menu"> 
  <div id="totals" class="totals" >
  </div> 
  <ul id="alter_menu"> 
  </ul> 
  <div class="content"> 
   <br /> 
   <table width="70%" style="margin-left: auto; margin-right: auto;"> 
   <tr> 
    <td class="major_text" align="center">
    <br/> 
    <span id="verbatim" class="sender"> Alexander</span>
    </td> 
   </tr> 
   <tr> 
   <td>
    </td> 
    </tr> 
   <tr> 
   <td class="newline"> 
  </td>
</div>
...

我运行的代码:

$dom = new domDocument($html);
$xpath = new domXPath($dom);
$nodes = $xpath->query('//span[@id="verbatim"]');
echo $nodes->item(0)->nodeValue;

问题是我一直收到 $nodes->item(0)->nodeValueNULL,我不确定如何检查这个 domElement。

当然,我需要Alexander

您只需实例化 DOMDocument,然后使用 ->loadHTML() 实际加载 HTML 标记:

$dom = new domDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html); // this line is important
$xpath = new domXPath($dom);
$nodes = $xpath->query('//span[@id="verbatim"]');
echo $nodes->item(0)->nodeValue;

Sample Output

->evaluate() 也可以:

echo $xpath->evaluate('string(//span[@id="verbatim"])');