Skip to content

Instantly share code, notes, and snippets.

Last active November 7, 2020 17:45
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
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