user1592380
user1592380

Reputation: 36317

how to echo content of nodes inclusing html tags using php domdocument

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

Upvotes: 3

Views: 6488

Answers (1)

Jon
Jon

Reputation: 437684

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.

Upvotes: 8

Related Questions