Last active
December 1, 2020 22:08
-
-
Save fabiocicerchia/1907339 to your computer and use it in GitHub Desktop.
PHP - Remove HTML entities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function unhtmlentities($string) { | |
$string = preg_replace_callback('~&#x([0-9a-f]+);~i', function ($matches) { return chr(hexdec($matches[0])); }, $string); | |
$string = preg_replace_callback('~&#([0-9]+);~', function ($matches) { return chr((int) $matches[0]); }, $string); | |
$trans_tbl = get_html_translation_table(HTML_ENTITIES); | |
$trans_tbl = array_flip($trans_tbl); | |
return strtr($string, $trans_tbl); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
updated to work with PHP 8.0.0