Home > Article > Backend Development > How to Insert HTML into PHP DOMNodes Without Encoding?
Inserting HTML into existing DOMNodes without content encoding can be achieved using the DOMDocumentFragment::appendXML method. Here's how:
// DOMDocument setup $dom = new DOMDocument; $dom->loadXml('<html><body/></html>'); $body = $dom->documentElement->firstChild; // Create DocumentFragment for template $template = $dom->createDocumentFragment(); $template->appendXML('<h1>
<?xml version="1.0"?> <html><body><h1>
If you need to import HTML from another DOMDocument, you can use the following code:
// Create new DOMDocument for template $tpl = new DOMDocument; $tpl->loadXml('<h1>
To import malformed HTML, use loadHTML instead of loadXml and enable internal error handling:
libxml_use_internal_errors(true); $tpl = new DOMDocument; $tpl->loadHtml('<h1>
The above is the detailed content of How to Insert HTML into PHP DOMNodes Without Encoding?. For more information, please follow other related articles on the PHP Chinese website!