我需要从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)->nodeValue
获得 NULL ,我不知道如何检查这个domElement。
当然,我需要Alexander
值
答案 0 :(得分:3)
您只需实例化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;
->evaluate()
也可以使用:
echo $xpath->evaluate('string(//span[@id="verbatim"])');