Skip to content

Instantly share code, notes, and snippets.

@fabiocicerchia
Last active December 1, 2020 22:08
Show Gist options
  • Save fabiocicerchia/1907339 to your computer and use it in GitHub Desktop.
Save fabiocicerchia/1907339 to your computer and use it in GitHub Desktop.
PHP - Remove HTML entities
<?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);
}
@fabiocicerchia
Copy link
Author

updated to work with PHP 8.0.0

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