Created
December 4, 2013 15:45
-
-
Save akkunchoi/7789794 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
| <?php | |
| // UTF-8文字列をUnicodeエスケープする。ただし英数字と記号はエスケープしない。 | |
| function unicode_encode($str) { | |
| return preg_replace_callback("/((?:[^\x09\x0A\x0D\x20-\x7E]{3})+)/", "encode_callback", $str); | |
| } | |
| function encode_callback($matches) { | |
| $char = mb_convert_encoding($matches[1], "UTF-16", "UTF-8"); | |
| $escaped = ""; | |
| for ($i = 0, $l = strlen($char); $i < $l; $i += 2) { | |
| $escaped .= "\u" . sprintf("%02x%02x", ord($char[$i]), ord($char[$i+1])); | |
| } | |
| return $escaped; | |
| } | |
| // Unicodeエスケープされた文字列をUTF-8文字列に戻す | |
| function unicode_decode($str) { | |
| return preg_replace_callback("/\\\\u([0-9a-zA-Z]{4})/", "decode_callback", $str); | |
| } | |
| function decode_callback($matches) { | |
| $char = mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UTF-16"); | |
| return $char; | |
| } | |
| // 使用例 | |
| $str = "東京"; | |
| $enc = unicode_encode($str); | |
| var_dump($enc); | |
| $dec = unicode_decode($enc); | |
| var_dump($dec); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment