PHP简单的HTML DOM解析器选择性根文本

时间:2015-04-28 15:53:32

标签: php parsing

您好我怎样才能在这段代码中只用简单的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>';

但它会打印每一个文字。

2 个答案:

答案 0 :(得分:1)

那是因为你选择了每个div而不是你想要的br标签,试试这个

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

答案 1 :(得分:0)

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

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

希望这有帮助。