Reputation: 546503
Using PHP's DOM functions, how do you convert a DOM Node's contents into a string?
<foo>
Blah blah <bar baz="1">bah</bar> blah blah
</foo>
Given foo
as the current context node, how do you get 'Blah blah <bar baz="1">bah</bar> blah blah'
as a string? Using $node->textContent
or $node->nodeValue
just returns the text nodes, not the <bar baz="1">
bits.
Basically, the equivalent of Javascript's innerHTML
property...
Upvotes: 3
Views: 476
Reputation: 302
You can create a new DOMDocument from the <foo>
node and parse it as XML or HTML:
function getInnerHTML($node) {
$tmpDoc = new DOMDocument('1.0');
$block = $tmpDoc->importNode($node->cloneNode(true),true);
$tmpDoc->appendChild($block);
$outer = $tmpDoc->saveHTML();
//this will remove the outer tags
return substr($outer,strpos($outer,'>')+1,-(strlen($node->nodeName)+4));
}
Upvotes: 5