How to append source html to a DOMElement in PHP?

fabio picture fabio · Jan 20, 2011 · Viewed 11.5k times · Source

Is there a way of appending source html into a DOMElement? Something like this:

$trElement->appendSource("<a href='?select_user=4'>Username</a>");

It would parse that fragment and then append it.

Answer

Gordon picture Gordon · Jan 20, 2011

You are looking for

- DOMDocumentFragment::appendXML — Append raw XML data

Example from Manual:

$doc = new DOMDocument();
$doc->loadXML("<root/>");
$f = $doc->createDocumentFragment();
$f->appendXML("<foo>text</foo><bar>text2</bar>");
$doc->documentElement->appendChild($f);
echo $doc->saveXML();