DOMDocument :: saveHTML ($ domnode) in PHP 5.2?

I have several functions in the class that return saveHTML (). After I repeat several functions in the saveHTML () class, it repeats some of the HTML. At first I solved this by running saveHTML ($ node), but now it does not seem to be an option.

I did not know that saveHTML ($ domnode) was only available in PHP 5.3.6, and I did not control the server on which I uploaded the files, so now I have to make it compatible with PHP 5.2.

For simplicity, and just to show my problem, it looks something like this:

<?php class HTML { private $dom; function __construct($dom) { $this->dom = $dom; } public function create_paragraph() { $p = $this->dom->createElement('p','Text 1.'); $this->dom->appendChild($p); return $this->dom->saveHTML(); } public function create_paragraph2() { $p = $this->dom->createElement('p','Text 2.'); $this->dom->appendChild($p); return $this->dom->saveHTML(); } } $dom = new DOMDocument; $html = new HTML($dom); ?> <html> <body> <?php echo $html->create_paragraph(); echo $html->create_paragraph2(); ?> </body> </html> 

Outputs:

 <html> <body> <p>Text 1.</p> <p>Text 1.</p><p>Text 2.</p> </body> 

I have an idea why this happens, but I have no idea how not to repeat it without saveHTML ($ domnode). How can I make it work with PHP 5.2?

Here is an example of what I want to do:

http://codepad.viper-7.com/o61DdJ

+7
source share
2 answers

What I'm doing is just save the node as XML. There are a few syntax differences, but this is good enough for most purposes:

 return $dom->saveXml($node); 
+10
source

You have return $this->dom->saveHTML(); twice in your class (as far as I know, you do not need to return it inside the class anywhere, unless this is a private function.

If you take return $this->dom->saveHTML(); from createparagraph() , it will echo without returning. This is a DOM thing, as far as I know, but new to this, like you.

0
source

All Articles