如何使用php简单的html dom解析器刮掉外部URL

时间:2012-03-29 06:09:39

标签: php html parsing dom

<div id="lyrics">
  <img />
  <span id="line_31" class="line line-s">En medio de este tropico mortal</span
  <br>
  <span id="line_32" class="line line-s">Roots and creation, come again!</span>
  <br> 
  <span id="line_33" class="line line-s">So mi guardian, mi guardian mi lift up di plan</span>
  <span id="line_34" class="line line-s">Now everybody a go' do dis one</span>
  <span id="line_35" class="line line-s">Like in down di Caribbean</span>
  <span id="line_36" class="line line-s">San Andrés, Providence Island</span>
  <br>
</div>

这里我有一个div,在div中,span之间有多个span和br标签。我想刮掉span文本和br标签。所以我怎么能用php简单的dom解析器。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

假设您上面的html文件名为“index.html”。

$html = file_get_html("index.html");
$element = $html->find('div#lyrics');
$result = $element->innertext;

您想查阅手册:http://simplehtmldom.sourceforge.net/