使用PHP Xpath在span之后获取文本?

时间:2014-10-02 09:12:10

标签: php html xpath

我有这样的HTML:

<h3><span itemprop="name">ABC</span> XYZ</h3>

我使用了xpath,但它只获取span标签(ABC)中的文本,现在我想在span之后获取文本。例如:我想获得XYZ

这是我的代码:

string = mb_convert_encoding($string, 'HTML-ENTITIES', "UTF-8");
    $dom = new DOMDocument();
    @$dom->loadHTML($string);
    $xpath = new DOMXPath( $dom );
    $nodelist = $xpath->query( "//h3/span[@itemprop='name']/text()" );     
    return $nodelist->item(0)->nodeValue;

并且答案是:ABC

1 个答案:

答案 0 :(得分:2)

尝试:

//h3/span[@itemprop='name']/following-sibling::text()