Created
April 25, 2014 12:47
-
-
Save xparq/11288458 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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: 中文"; | |
$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