[ACCEPTED]-Php and Xpath - how to get text from inside a node-xpath

Accepted answer
Score: 12

For SimleXmlElement just cast it as a string: return (string) $temp;. For DomDocument use 1 return $temp->nodeValue.

Score: 10

Try:

$dom = new DOMDocument;
$dom->loadXML('<root>
    <p>some text<br />some more text</p>
</root>');

$xpath = new DOMXPath($dom);
foreach ($xpath->query('/root/p/text()') as $textNode) {
    echo $textNode->nodeValue;
}

$textNode will be a DOMText.

0

Score: 2
(string) current($xml->xpath("//group[@ref='HS_TYP']"))

Hope that helps

0

Score: 2

This will get all inner text from simple 1 xml element

dom_import_simplexml($xpath->query('//div[@class="h1"]')->textContent;

More Related questions