如何通过 DOMDocument 获取元素的正文
How to get the main text of an element by DOMDocument
我想获取不带 DOMDocument
子元素的元素的主文本。例如,
<span>
This is the main
<i>more</i>
<b>extra</b>
<span>
我通过
获取文本
$text=$g->query('//span')[0]->nodeValue;
但是我怎样才能只得到span
下面的文本值呢?在这里,只有 This is the main
文本忽略任何子元素。
echo $g->query('//span/text()')[0]->nodeValue;
输出为:
This is the main
这只会找到 <span>
.
的直接子节点的文本节点
我想获取不带 DOMDocument
子元素的元素的主文本。例如,
<span>
This is the main
<i>more</i>
<b>extra</b>
<span>
我通过
获取文本$text=$g->query('//span')[0]->nodeValue;
但是我怎样才能只得到span
下面的文本值呢?在这里,只有 This is the main
文本忽略任何子元素。
echo $g->query('//span/text()')[0]->nodeValue;
输出为:
This is the main
这只会找到 <span>
.