1. JSON_UNESCAPED_UNICODE
echo json_encode("我是中文"); // "\u6211\u662f\u4e2d\u6587"
echo json_encode("我是中文", JSON_UNESCAPED_UNICODE); // "我是中文"
$array = array(
'name' => 'lv简简',
'gender' => '女',
'age' => 20
);
echo json_encode($array); // {"name":"lv\u7b80\u7b80","gender":"\u5973","age":20}
echo json_encode($array, JSON_UNESCAPED_UNICODE); // {"name":"lv简简","gender":"女","age":20}
2. urlencode、urldecode
$a = urlencode("我是中文");
$b = json_encode($a);
echo urldecode($b); // "我是中文"
$array = array(
'test'=>urlencode("我是中文")
);
$array = json_encode($array);
echo urldecode($array); // {"test":"我是中文"}
3. decodeUnicode
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);
}
$array = array(
'name' => 'lv简简',
'gender' => '女',
'age' => 20
);
echo json_encode($array); // {"name":"lv\u7b80\u7b80","gender":"\u5973","age":20}
echo decodeUnicode(json_encode($array)); // {"name":"lv简简","gender":"女","age":20}