Search code examples
phphtmldomappendchild

How to append source html to a DOMElement in PHP?


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.


Solution

  • 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();