Skip to content

Instantly share code, notes, and snippets.

@pixeline
Last active April 28, 2019 13:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixeline/41652175f5e5f92b03f2114c9e988fd8 to your computer and use it in GitHub Desktop.
Save pixeline/41652175f5e5f92b03f2114c9e988fd8 to your computer and use it in GitHub Desktop.
PHP function removing all empty html nodes from a given html fragment. Useful for cleaning content entered via WYSIWYG editors.
<?php
function remove_empty_html_tags($html)
{
/**
* PHP function removing all empty html nodes from a given html fragment. Useful for cleaning content entered via WYSIWYG editors.
* source: https://stackoverflow.com/a/42067229/53960
* Regex by Mark Notton
*/
$regex = '~<((?!iframe|canvas)\w+)[^>]*>(?>[\p{Z}\p{C}]|<br\b[^>]*>|&(?:(?:nb|thin|zwnb|e[nm])sp|zwnj|#xfeff|#xa0|#160|#65279);|(?R))*</\1>~iu';
return preg_replace($regex, '', $html);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment