php处理json_encode汉语UNICODE转换格式难题
用PHP的json_encode来解决汉语的情况下, 汉语都是被编号, 变为不能读的, 相近 \u*** 的文件格式,假如想中国汉字不开展转换格式,这儿出示三种方式
1.升級PHP,在PHP5.4, 这一难题总算足以处理, Json增加了一个选择项: JSON_UNESCAPED_UNICODE, 故名思议, 便是说, Json不必编号Unicode.
lt;?ph
echo json_encode( 汉语 , JSON_UNESCAPED_UNICODE)
// 汉语"
2.把中国汉字先urlencode随后再应用json_encode,json_encode以后再度应用urldecode来编解码,那样编号出去的json数字能量数组中的中国汉字也不会出現unicode编号了。
$array = array(
'test'= urlencode( 我是检测 )
)
$array = json_encode($array)
echo urldecode($array)
//{ test : 我是检测 }
3.对unicode码再开展编解码,编解码涵数以下:
function decodeUnicode($str)
{
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
create_function(
'$matches',
'return mb_convert_encoding(pack( H* , $matches[1]), UTF-8 , UCS-2BE '
),
$str)
}
4.事例
$arr = array('name1': 汉语 ,'name2':'abc12')
$jsonstr = decodeUnicode(json_encode($arr))
共享到: