- mb_convert_encoding: documentation ( source)
- libxml_use_internal_errors: documentation ( source)
<?php
$html = <<<HTML
<script>
console.log('<h1>aaa<\/h1>');
</script>
HTML;
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
echo $dom->saveHTML();