在 PHP DOM 中获取节点的文本 [英] Getting node's text in PHP DOM
本文介绍了在 PHP DOM 中获取节点的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 PHP DOM 从此标记中提取字符串text"?
notthistext
$div->nodeValue
包括notthis"
解决方案
只要你能影响 DOM,你就可以移除那个 span
.
$span = $div->getElementsByTagName('span')->item(0);$div->removeChild($span);$nodeValue = $div->nodeValue;
或者,直接访问$div
的文本节点.
foreach($div->childNodes as $node) {如果 ($node->nodeType != XML_TEXT_NODE) {继续;}$nodeValue = $node;}
如果你最终有更多的文本节点并且只想要第一个,你可以在 $nodeValue
的第一次赋值之后break
.
How could I extract the string "text" from this markup using the PHP DOM?
<div><span>notthis</span>text</div>
$div->nodeValue
includes "notthis"
解决方案
So long as you can affect the DOM, you could remove that span
.
$span = $div->getElementsByTagName('span')->item(0);
$div->removeChild($span);
$nodeValue = $div->nodeValue;
Alternatively, just access the text node of $div
.
foreach($div->childNodes as $node) {
if ($node->nodeType != XML_TEXT_NODE) {
continue;
}
$nodeValue = $node;
}
If you end up with more text nodes and only want the first, you can break
after the first assignment of $nodeValue
.
这篇关于在 PHP DOM 中获取节点的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文