這是一個將漢字轉換成Unicode編碼的PHP函數,支援GBK和UTF8編碼。
function uni_decode ($uncode)
{
$word = json_decode(preg_replace_callback('/&#(\d{5});/', create_function('$dec', 'return \'\\u\'.dechex($dec[1]);'), '"'.$uncode.'"'));
return $word;
}
對 Unicode 轉換為漢字
function uni_decode ($uncode)
{
$word = json_decode(preg_replace_callback('/&#(\d{5});/', create_function('$dec', 'return \'\\u\'.dechex($dec[1]);'), '"'.$uncode.'"'));
return $word;
}
以下樣本示範:
function uni_encode ($word)
{
$word0 = iconv('gbk', 'utf-8', $word);
$word1 = iconv('utf-8', 'gbk', $word0);
$word = ($word1 == $word) ? $word0 : $word;
$word = json_encode($word);
$word = preg_replace_callback('/\\\\u(\w{4})/', create_function('$hex', 'return \'&#\'.hexdec($hex[1]).\';\';'), substr($word, 1, strlen($word)-2));
return $word;
}
$word = '將朱祖松和霍濤轉為 Unicode 四位元組編碼! ';
echo uni_encode($word);
上述將輸出如下結果:
本文連結http://www.cxybl.com/html/wlbc/Php/20130326/37396.html