Skip to content Skip to sidebar Skip to footer

Xpath NodeValue/textContent Unable To See
Tag

HTML is as follows: ABC
DEF
However, both nodeValue and textContent attributes show 'ABCDEF' as the value. Any way to show or parse the

Solution 1:

Maybe this'll help you: DOMNode::C14N

It'll return the HTML of the node.

<?php
$a = '<a href="#">ABC<BR>DEF</a>';
$doc = new DOMDocument();
@$doc->loadHTML($a);
$finder = new DomXPath($doc);
$nodes = $finder->query("//a");
foreach ($nodes as $node) {
    var_dump($node->c14n());
}

Demo


Solution 2:

I know you have already solved your problem, but I wanted to add a more direct way of solving it...

$a = '<a href="#">ABC<BR>DEF</a>';
$doc = new DOMDocument();
$doc->loadHTML($a);
$xp = new DomXPath($doc);
$nodes = $xp->query("//a/node()");
$text = '';
foreach ($nodes as $node) {
     $text .= $doc->saveHTML($node);
}
echo $text;

Outputs...

ABC<br>DEF

Post a Comment for "Xpath NodeValue/textContent Unable To See
Tag"