0

How to create a DOM from a User's input in PHP5?

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.