Skip to content

Instantly share code, notes, and snippets.

@eusonlito
Last active November 7, 2020 17:45
Show Gist options
  • Save eusonlito/6486792 to your computer and use it in GitHub Desktop.
Save eusonlito/6486792 to your computer and use it in GitHub Desktop.
This function allow to repair bad partial HTML (without DOCTYPE, html, head or body tags).
<?php
function fixHtml ($html)
{
libxml_use_internal_errors(true);
$DOM = new \DOMDocument;
$DOM->recover = true;
$DOM->preserveWhiteSpace = false;
$DOM->substituteEntities = false;
$DOM->loadHtml('<?xml encoding="UTF-8">'.$html, LIBXML_NOBLANKS | LIBXML_ERR_NONE);
libxml_use_internal_errors(false);
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