本文實例講述了PHP實現(xiàn)json_decode不轉(zhuǎn)義中文的方法。分享給大家供大家參考,具體如下:
	默認情況下PHP的 json_decode 方法會把特殊字符進行轉(zhuǎn)義,還會把中文轉(zhuǎn)為Unicode編碼形式。
這使得數(shù)據(jù)庫查看文本變得很麻煩。所以我們需要限制對于中文的轉(zhuǎn)義。
對于PHP5.4+版本,json_decode函數(shù)第二個參數(shù),可以用來限制轉(zhuǎn)義范圍。
	要限制中文,使用JSON_UNESCAPED_UNICODE參數(shù)。
json_encode($a, JSON_UNESCAPED_UNICODE);
對于PHP5.3版本,可以先把ASCII 127以上的字符轉(zhuǎn)換為HTML數(shù)值,這樣避免被json_decode函數(shù)轉(zhuǎn)碼:
function my_json_encode($arr){    //convmap since 0x80 char codes so it takes all multibyte codes (above ASCII 127). So such characters are being "hidden" from normal json_encoding    array_walk_recursive($arr, function (&$item, $key) { if (is_string($item)) $item = mb_encode_numericentity($item, array (0x80, 0xffff, 0, 0xffff), 'UTF-8'); });    return mb_decode_numericentity(json_encode($arr), array (0x80, 0xffff, 0, 0xffff), 'UTF-8');}希望本文所述對大家PHP程序設(shè)計有所幫助。
新聞熱點
疑難解答
圖片精選