Skip to content

Instantly share code, notes, and snippets.

@lutzissler
Created December 18, 2018 22:54
Show Gist options
  • Save lutzissler/3e6f19a6f86bb9e36f364b4e1f4b690c to your computer and use it in GitHub Desktop.
Save lutzissler/3e6f19a6f86bb9e36f364b4e1f4b690c to your computer and use it in GitHub Desktop.
Close any open tags in HTML string
function closetags($html) {
libxml_use_internal_errors(true);
$dom = new \DOMDocument;
$dom->loadHTML($html);
// Strip wrapping <html> and <body> tags
$mock = new \DOMDocument;
$body = $dom->getElementsByTagName('body')->item(0);
foreach ($body->childNodes as $child) {
$mock->appendChild($mock->importNode($child, true));
}
return trim($mock->saveHTML());
}
@lutzissler
Copy link
Author

This is taken from https://gist.github.com/JayWood/348752b568ecd63ae5ce#gistcomment-2184548 – thanks for the amazing snippet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment