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



我有一些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 标签的文本?

试试这个:

foreach($xp->query('//div[@class="mytext"]/text()[normalize-space()]') as $node) {
$description = $node->nodeValue;
echo $description; 
}

相关内容

  • 没有找到相关文章

最新更新