转换从Facebook图形API返回的UTF8字符


Convert UTF8 characters returned from Facebook Graph API

字符为UTF8编码,如..

" ' u676f ' u845b "

如何将其转换回PHP中的正常UTF8字符串?

简单的方法是将字符串包装成双引号,让json_decode转换'u0000的转义。(这恰好是Javascript字符串语法)

 $str = json_decode("'"$str'"");

似乎是亚洲字母:杯葛(当json_decode返回它时已经是UTF-8)

(源)

http://webarto.com/83/php-unicode_decode-5.3演示:http://ideone.com/AtY0v

function decode_encoded_utf8($string){
    return preg_replace_callback('#''''u([0-9a-f]{4})#ism', function($matches) { return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE"); }, $string);
}
 
echo unicode_decode(''u676f'u845b'); # 杯葛