I have a Domnodelist of html nodes, which I would like to echo out including their html tags so that I can apply a regex.When I do:
foreach ($nodeList as $key => $node) {
echo $node->nodeValue;
}
I get only the text portions of the nodes, without html tags.
when I try:
echo $node->saveHTML;
I get the error: Undefined property: DOMElement::$saveHTML
How can I do this?
Thanks in advance,
Bill
saveHTML
is a function, so you need to call it (you were missing the parens). It's also part of DOMDocument
; the node will be passed in as a parameter.
So the correct form is:
echo $document->saveHTML($node);
Important note: saveHTML
only supports a parameter from PHP 5.3.6 onwards.
However, people use DOMDocument
specifically so that they can process it without resorting to regular expressions. It seems that here you are trying to do the opposite. If so, reconsider your approach -- regular expressions are a hacky way to process HTML that will bring trouble the second you decide to do anything that is not totally trivial.