如何通过DOMDocument获取元素的主要文本

时间:2019-07-09 12:43:06

标签: php xpath 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仅通过忽略任何子元素而发短信。

1 个答案:

答案 0 :(得分:2)

使用text() node test

echo $g->query('//span/text()')[0]->nodeValue;

输出为:

This is the main

这只会找到<span>的直接子节点的文本节点。

相关问题