我需要在此处提取 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'><< 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"><< 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...
关于php - 简单的 HTML Dom - 查找 div 之间的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21839709/