Answer the question
In order to leave comments, you need to log in
XPath: how to get a text node when there are tags next to it?
Hello!
I started learning XPath, a very cool thing, but it stuck out of the blue ... There is a code:
$xml = simplexml_load_string('<div>123 <br /> 456</div>');
$xml->xpath('//div/text()[1]')
Answer the question
In order to leave comments, you need to log in
$dom = new DOMDocument;
$dom->loadXML('<div>123 <br /> 456</div>');
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//div/text()') as $textNode) {
echo trim($textNode->nodeValue), PHP_EOL;
}
ideone.com/FYkZwX
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question