We know that with PHP Json_encode to deal with Chinese, Chinese will be encoded, become unreadable, similar to the "\u***" format, but also to some extent increase the amount of data transmitted.
<?php
Echo Json_encode ("Chinese");
"\u4e2d\u6587"
This let us do development in the celestial students, is very headache, sometimes also have to write their own json_encode.
In PHP5.4, the problem was finally resolved, and JSON added an option: Json_unescaped_unicode, hence the name incredible, which means that JSON does not encode UNICODE.
Look at the following example:
<?php
Echo Json_encode ("Chinese", Json_unescaped_unicode);
Chinese
What, is it a change that makes everyone happy? Oh, of course, JSON also joins in 5.4: Json_bigint_as_string, Json_pretty_print, json_unescaped_slashes, etc.
HP5.4 only supports the Json_unescaped_unicode parameter, which allows Chinese characters to be json_encode without escaping, reducing the amount of data transferred. But in PHP5.3, you have to write a function to implement, the following is the solution:
/**
* JSON Encoding of variables
* @param mixed value to be encoded, except for the resource type, can be any data type, the function can only accept UTF-8 encoded data
* @return String returns the JSON form of value
*/
function json_encode_ex ($value)
{
if (Version_compare (php_version, ' 5.4.0 ', ' < '))
{
$str = Json_encode ($value);
$str = Preg_replace_callback (
"#\\\u ([0-9a-f]{4}) #i",
function ($matchs)
{
Return Iconv (' Ucs-2be ', ' UTF-8 ', pack (' H4 ', $matchs [1]));
},
$str
);
return $str;
}
Else
{
Return Json_encode ($value, Json_unescaped_unicode);
}
}