Skip to content

Instantly share code, notes, and snippets.

@eriksape
Forked from joshmfrankel/fixBrokenHTMLString.php
Created August 14, 2018 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eriksape/0a0973ae70616e3547258155d84bae13 to your computer and use it in GitHub Desktop.
Save eriksape/0a0973ae70616e3547258155d84bae13 to your computer and use it in GitHub Desktop.
PHP: Fix broken html tags in string and use html entities
/**
* Fix for broken html tags inside strings for php.
*
* By using passing the html through DOMDocument and converting the html
* entities we are left with beautiful well formatted code. Huzzah!
*
* "Nothing is ever easy" -Zedd, Wizards First Rule
*
* @var DOMDocument
*/
$doc = new DOMDocument();
$doc->substituteEntities = false;
$content = mb_convert_encoding($sValue, 'html-entities', 'utf-8');
$doc->loadHTML($content);
$sValue = $doc->saveHTML();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment