开发者

php: How to get the unicode character from the STRING "U4e9c"?

This doesn't work (just echoes "U4e9c"):

echo mb_convert_encoding("U4e9c","UTF-8","auto");

I guess some sort of castin开发者_Go百科g "U4e9c" is needed, but can't figure out how...


this comment provides two functions because unicode_decode() does not seem to exist in php5. Here are my tests, it seems to work:

greg@liche :) ~ > php -a
Interactive shell

php > function unicode_decode($str){                                           
php {     return preg_replace(
php (         '#\\\u([0-9a-f]{4})#e',
php (         "unicode_value('\\1')",
php (         $str);
php { }
php > 
php > function unicode_value($code) {
php {     $value=hexdec($code);
php {     if($value<0x0080)
php {         return chr($value);
php {     elseif($value<0x0800)
php {         return chr((($value&0x07c0)>>6)|0xc0)
php {             .chr(($value&0x3f)|0x80);
php {     else
php {         return chr((($value&0xf000)>>12)|0xe0)
php {         .chr((($value&0x0fc0)>>6)|0x80)
php {         .chr(($value&0x3f)|0x80);
php { } 
php > echo unicode_decode('\u4e9c');
亜
php > echo mb_convert_encoding(unicode_decode('\u4e9c'),  "UTF-8", "auto");
亜


This seems to work fine:

echo "&#" . hexdec(str_replace("U","","U4e9c")) . ";";

Update

Here is where the mb_convert_encoding comes in:

$k = "&#" . hexdec(preg_replace("/[Uu]/","","U4e9c")) . ";";
$k=mb_convert_encoding($k ,"UTF-8","HTML-ENTITIES");

This allows me to UPDATE my mysql DATABASE with $k, (whereas without mb_convert_encoding it only works for DISPLAYING inside an HTML page).


function utf8chr($i) {
    return iconv('UCS-4LE', 'UTF-8', pack('V', $i));
}

echo utf8chr(hexdec(substr('U4e9c', 1)));  // echo utf8chr(0x4E9C)


See this: Unicode character in PHP string

Because JSON directly supports the \uxxxx syntax the first thing that comes into my mind is:

$unicodeChar = '\u1000'; echo json_decode('"'.$unicodeChar.'"');

echo json_decode('"\\'."U4e9c".'"');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜