Asamoa
Asamoa

Reputation: 149

getting text after span using PHP Xpath?

I have html like this :

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

I used xpath but it only get the text in span tag (ABC) , now i want to get text after span . Ex: I want to get XYZ

Here is my code :

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;

And ressult is : ABC

Upvotes: 1

Views: 1980

Answers (1)

paul trmbrth
paul trmbrth

Reputation: 20748

Try:

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

Upvotes: 4

Related Questions