PHP Simple HTML DOM Parser 选择性根文本



嗨,我怎么能在这个代码中只获得选择性文本,而简单的php解析器

<div class="body" style="text-align: justify;padding: 10px;">
        <a class="entekhab_lead2" href="/"> other text : </a>
        <br> I want this text <br>
        <div align="justify"> other text or elements <div>
        <div> other text or elemnets </div>
        and anything else....
</div>
我想要

只返回这个:我想要这个文本

我试试这个

foreach($html->find('div') as $element) 
   echo $element->plaintext . '<br>';

但它打印了每个文本。

那是因为您选择的是每个div 而不是您想要的 br 标签,请尝试此操作

foreach($html->find('br') as $element) 
  echo $element->plaintext . '<br>';

另一种方法,看看 php strip_tags()。此函数允许删除带有允许参数的 html 标记。

ie. strip_tags($string, '<br><br/>');
implies remove all tags leaving <br>'s

希望这有帮助。

最新更新