php - 使用 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;
}
推荐阅读
- c - UVa 1225“数字计数”的 C“运行时错误”
- javascript - 后期“手动”将元素升级为定制的内置 Web 组件
- excel - 使用VBA检索特定列中同一行中单元格的值
- node.js - Typescript 和 Jest:避免模拟函数上的类型错误
- python - 以正确的格式获取文本以用于 spacy
- sql-server - 如何衡量 Azure SQL DB 的性能?
- android - ViewRenderable not working ARCore
- javascript - 如何识别已标记为 Redact 的 PDF 中的原始单词?
- css - 不同数量的 div 布局
- php - 通过 PHP 从 url 获取 JSON 数据