Skip to content

Instantly share code, notes, and snippets.

@akkunchoi
Created December 4, 2013 15:45
Show Gist options
  • Select an option

  • Save akkunchoi/7789794 to your computer and use it in GitHub Desktop.

Select an option

Save akkunchoi/7789794 to your computer and use it in GitHub Desktop.
<?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