使用DomDocument检索文本,但删除内部的h1标签

时间:2018-07-07 21:48:29

标签: php xpath domdocument

我有一些html试图检索文本,但没有<h1>标签内容。

$html = '<div class="mytext">   
           <h1>Title of document</h1>   
           This is the text that I want, without the title.
         </div>';

$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$xp = new DOMXpath($dom);
foreach($xp->query('//div[@class="mytext"]') as $node) {
  $description = $node->nodeValue;
  echo $description; 
}

最终结果应为:This is the text that I want, without the title.

当前为:Title of document This is the text that I want, without the title

我如何只获取没有h1标签的文本?

1 个答案:

答案 0 :(得分:1)

尝试一下:

y