Skip to content

Instantly share code, notes, and snippets.

@xparq
Created April 25, 2014 12:47
Show Gist options
  • Save xparq/11288458 to your computer and use it in GitHub Desktop.
Save xparq/11288458 to your computer and use it in GitHub Desktop.
From http://www.php.net/manual/en/function.chr.php#55978:
"I spent hours looking for a function which would take a numeric HTML entity value and output the appropriate UTF-8 bytes.
I found this at another site and only had to modify it slightly; so I don't take credit for this."
<?php function unichr($dec) {
if ($dec < 128) {
$utf = chr($dec);
} else if ($dec < 2048) {
$utf = chr(192 + (($dec - ($dec % 64)) / 64));
$utf .= chr(128 + ($dec % 64));
} else {
$utf = chr(224 + (($dec - ($dec % 4096)) / 4096));
$utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));
$utf .= chr(128 + ($dec % 64));
}
return $utf;
} ?>
So for example:
<?php
$str = "Chinese: &#20013;&#25991;";
$str = preg_replace("/&#(\d{2,5});/e", "unichr($1);", $str);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment