Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert HTML to PHP DOMNode?

Is there any way I can insert an HTML template to existing DOMNode without content being encoded?

I have tried to do that with:

$dom->createElement('div', '<h1>Hello world</h1>'); $dom->createTextNode('<h1>Hello world</h1>'); 

The output is pretty much the same, with only difference that first code would wrap it in a div. I have tried to loadHTML from string but I have no idea how can I append it's body content to another DOMDocument.

In javascript, this process seems to be quite simple and obvious.

like image 689
Nazariy Avatar asked Dec 09 '10 17:12

Nazariy


1 Answers

You can use

  • DOMDocumentFragment::appendXML — Append raw XML data

Example:

// just some setup $dom = new DOMDocument; $dom->loadXml('<html><body/></html>'); $body = $dom->documentElement->firstChild;  // this is the part you are looking for     $template = $dom->createDocumentFragment(); $template->appendXML('<h1>This is <em>my</em> template</h1>'); $body->appendChild($template);  // output echo $dom->saveXml(); 

Output:

<?xml version="1.0"?> <html><body><h1>This is <em>my</em> template</h1></body></html> 

If you want to import from another DOMDocument, replace the three lines with

$tpl = new DOMDocument; $tpl->loadXml('<h1>This is <em>my</em> template</h1>'); $body->appendChild($dom->importNode($tpl->documentElement, TRUE)); 

Using TRUE as the second argument to importNode will do a recursive import of the node tree.


If you need to import (malformed) HTML, change loadXml to loadHTML. This will trigger the HTML parser of libxml (what ext/DOM uses internally):

libxml_use_internal_errors(true); $tpl = new DOMDocument; $tpl->loadHtml('<h1>This is <em>malformed</em> template</h2>'); $body->appendChild($dom->importNode($tpl->documentElement, TRUE)); libxml_use_internal_errors(false); 

Note that libxml will try to correct the markup, e.g. it will change the wrong closing </h2> to </h1>.

like image 123
Gordon Avatar answered Sep 19 '22 15:09

Gordon