php - 使用 php 和 xpath 在 html 标记后获取文本

标签 php html xpath

我从网页的 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/

相关文章:

php - Laravel 5,子域路由,带可选参数

python - lxml 移动带有元素的文本

google-sheets - 无法使用 ImportXML xpath 查询提取嵌套数据 - 谷歌表格

xslt - XSL : Find text not currently in elements and wrap?

php/mysql 随机排序日期

php - 使用 PHP 发送 Outlook 2010 电子邮件

PHP 将变量附加到字符串的末尾

selenium - 在 Xpath 中使用 OR 条件来标识相同的元素

php - 如何显示上周的项目?

html - 网络音频API : Merging stream of mp3 sounds to one file and playing it in <audio> tag