php - 简单的 HTML Dom - 查找 div 之间的文本

标签 php simple-html-dom

我需要在此处提取 div 之间的文本(“四个中的第三个...”) - 使用 Simple HTML Dom PHP 库。

我已经尝试了我所想到的一切! next_sibling()返回评论,并且 next_sibling()->next_sibling()返回 <br/>标签。理想情况下,我想获取从第一条评论末尾到下一条 </div> 的所有文本。标签。

<div class="left">
Bla-bla..
<div class="float">Bla-bla...
</div><!--/end of div.float-->
    <br />The third of four performances in the Society's Morning Melodies series features...<a href='index.php?page=tickets&month=20140201'>&lt;&lt; Back to full event listing</a>
</div><!--/end of div.left-->

下面打印 <!--/end of div.float--> - 评论标签。

//find content that follows div with a class float. There is a comment in between.
$div_float = $html->find("div.float");
$betweendivs =  $div_float[0]->next_sibling();
$actual_content = $betweendivs ->outertext ;
echo $actual_content;

我的下一步是获取innertext div.left 的,然后删除其中的所有 div,但这似乎是一个很大的麻烦。有什么更简单的事情可以做吗?

最佳答案

使用 find('text', $index) 获取所有文本 block ,其中 $index 是所需文本的索引...

所以在这种情况下,它是:

echo $html->find('text', 3);

// OUTPUT:
The third of four performances in the Society's Morning Melodies series features...

您可以在 Manual 中阅读更多内容

编辑:

这是一个工作代码:

$input = '<div class="left">
Bla-bla..
<div class="float">Bla-bla...
</div><!--/end of div.float-->
    <br />The third of four performances in the Society\'s Morning Melodies series features...<a href="index.php?page=tickets&month=20140201">&lt;&lt; Back to full event listing</a>
</div><!--/end of div.left-->';

//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a string
$html->load($input);

// Using $index
echo $html->find('text', 3);

echo "<hr>";

// Or, it's the 3rd element starting from the end
$text = $html->find('text');
echo $text[count($text)-3];

// Clear DOM object
$html->clear();
unset($html);

// OUTPUT
The third of four performances in the Society's Morning Melodies series features...
The third of four performances in the Society's Morning Melodies series features...

Working DEMO

关于php - 简单的 HTML Dom - 查找 div 之间的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21839709/

相关文章:

javascript - 如何从 Bootstrap Datepaginator 禁用过去的日期

php - 有没有办法以交互方式安装建议的 Composer 包?

php - 加载mysql时先显示主要内容然后显示菜单

php - 如果多维数组中的键值对匹配,如何合并两个具有平均值的数组

PHP while 语句问题

javascript - 通过简单的html dom删除页面的所有href

php - 如何使用 PHP Simple HTML DOM Parser 添加自定义属性

javascript - simplehtmldom 触发点击元素

php - 使用 PHP 简单 HTML DOM 解析器获取 Div 属性

php - 在 PHP 中删除表上的行跨度