Home » Php » dom – Disable html entity encoding in PHP DOMDocument

dom – Disable html entity encoding in PHP DOMDocument

Posted by: admin July 12, 2020 Leave a comment

Questions:

I cannot figure out how to stop DOMDocument from mangling these characters.

<?php

$doc = new DOMDocument();
$doc->substituteEntities = false;
$doc->loadHTML('<p>¯\(°_o)/¯</p>');
print_r($doc->saveHTML());

?>

Expected Output:
¯(°_o)/¯

Actual Output:
¯(°_o)/¯

http://codepad.org/W83eHSsT

How to&Answers:

I’ve found a hint in the comments on http://php.net/manual/en/domdocument.loadhtml.php

(Comment from <mdmitry at gmail dot com> 21-Dec-2009 05:02: “You can also load HTML as UTF-8 using this simple hack:”)

Just add '<?xml encoding="UTF-8">' before the HTML-input:

$doc = new DOMDocument();
//$doc->substituteEntities = false;
$doc->loadHTML('<?xml encoding="UTF-8">' . '<p>¯\(°_o)/¯</p>');
print_r($doc->saveHTML());

Answer:

<?xml version="1.0" encoding="utf-8">

in the top of the document takes care of tags.. for both saveXML and saveHTML.