我从网页的 curl 碎片中获得了以下 html:
<div id="box">
<br>
Your word(s):
<br>
<br>
functionally
<br>
<br>
<br>
我想要第三个之后的东西 <br>
: /html/body/div[2]/div/br[3]
- 即功能
@$itemCell = $xpath->query( "/html/body/div[2]/div/br[3]" );
$word = $itemCell->item( 0 );
return $word->nodeValue;
这不会返回任何东西。如果我备份到 /div
我当然得到了 box 的全部内容。如何提取第二个 <br>
之后的单词.我的话总是在第三次之后 <br>
.
看起来很简单,但它让我难以理解。
最佳答案
尝试类似这样的查询
$textNodes = $xpath->query('//div[@id="box"]/br[3]/following-sibling::text()[1]');
此处的工作演示 - http://codepad.viper-7.com/00oeZh
这里的关键是following-sibling
Axes .
关于php - 使用 php 和 xpath 在 html 标记后获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12169275/