Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This function allow to repair bad partial HTML (without DOCTYPE, html, head or body tags).
function fixHtml ($html)
$DOM = new \DOMDocument;
$DOM->recover = true;
$DOM->preserveWhiteSpace = false;
$DOM->substituteEntities = false;
$DOM->loadHtml('<?xml encoding="UTF-8">'.$html, LIBXML_NOBLANKS | LIBXML_ERR_NONE);
return preg_replace('~<(?:!DOCTYPE|/?(?:\?xml|html|head|body))[^>]*>\s*~i', '', $DOM->saveHTML());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment