I need to parse out of my html the contents of the <span id="the_name">
tag.
the html looks like this:
我需要解析我的html 标签的内容。 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>
...
the code I run:
我运行的代码:
$dom = new domDocument($html);
$xpath = new domXPath($dom);
$nodes = $xpath->query('//span[@id="verbatim"]');
echo $nodes->item(0)->nodeValue;
Problem is I keep getting a NULL for $nodes->item(0)->nodeValue
, I am not sure how to inspect this domElement.
问题是我一直为$ nodes-> item(0) - > nodeValue获取NULL,我不知道如何检查这个domElement。
Of course, I need the Alexander
value
当然,我需要亚历山大的价值
1 个解决方案
#1
2
You just instantiate the DOMDocument
then use ->loadHTML()
to actually load the HTML markup:
您只需实例化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()
will also work as well:
- > evaluate()也可以正常工作:
echo $xpath->evaluate('string(//span[@id="verbatim"])');
#1
2
You just instantiate the DOMDocument
then use ->loadHTML()
to actually load the HTML markup:
您只需实例化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()
will also work as well:
- > evaluate()也可以正常工作:
echo $xpath->evaluate('string(//span[@id="verbatim"])');