How to create a DOM from a User's input in PHP5?
3 Answers
I would use the DOM API that has been part of the core since 5. For an XML string $xml, you can build a DOM object with
$dom = new DOMDocument();
$dom->loadXML($xml);
Manipulate it with the rest of the DOM API, defined at http://uk.php.net/DOM
Comments
And when you need to inject it back into some other DOM (like your HTML page) you can export it again using the $dom->saveXML() method. The problem however is that it also exports an xml header (it's even worse for the saveHTML version). To get rid of that use this:
$xml = $dom->saveXML();
$xml = substr( $xml, strlen( "<?xml version=\"1.0\"?>" ) );
Comments
If the input is HTML, use the loadHTML method. Be ware that the input has to be valid code, so you might want to pipe it through html tidy first.