In PHP json_encode Chinese display problem is a difficult problem for many programmers, the following I will give you two Chinese display problems in the solution, we can refer to.
JSON has become the most commonly used data format for Web development, and PHP supports JSON and array conversion functions Json_encode and Json_decode starting from 5.2. But in the course of use we will find, (below with "You" the Chinese character as an example) through the Json_encode function after the conversion of Chinese all become similar to U4F60 (you) such a code, although does not affect program execution, but very not intuitive
First of all, Json_encode's processing of Chinese is turned into the hexadecimal representation of the corresponding Unicode code U4F60, (and JS's escape function is similar (%U4F60)), that is, 0x4f60. Therefore, we only need to convert the Unicode code (UCS-2) to utf-8 encoded kanji. The functions are as follows:
| The code is as follows |
Copy Code |
/** * Json_encode Support Chinese version * @param mixed $data parameters are exactly the same as Json_encode */ function Json_encode_cn ($data) { $data = Json_encode ($data); Return Preg_replace ("/\u ([0-9a-f]{4})/ie", "Iconv (' UCS-2 ', ' UTF-8 ', pack (' h* ', ' $")); ", $data); } |
Here, the target data is first converted to the JSON string of Unicode encoding code, and then the corresponding four-bit letters are replaced with the corresponding U-text, then re-transcoding. The E in the Preg_replace regular allows the second parameter to perform the eval operation, first matching the uxxxx, then passing the hexadecimal value XXXX to the Unicode encoded character through the pack function, then turning the Unicode code into a utf-8 code, and then you can see The normal kanji.
Another solution that Json_encode () does not support Chinese characters
| The code is as follows |
Copy Code |
/** * UrlEncode processing of array and scalar * Usually call Wphp_json_encode () * Handle Json_encode Chinese display problem * @param array $data * @return String */ function Wphp_urlencode ($data) { if (Is_array ($data) | | is_object ($DATA)) { foreach ($data as $k = = $v) { if (Is_scalar ($v)) { if (Is_array ($data)) { $data [$k] = UrlEncode ($v); } else if (Is_object ($data)) { $data $k = UrlEncode ($v); } } else if (Is_array ($data)) { $data [$k] = Wphp_urlencode ($v); Call this function recursively } else if (Is_object ($data)) { $data $k = Wphp_urlencode ($v); } } } return $data; } /** * JSON encoding * * Solve Chinese after json_encode () processing after the display is not intuitive situation * If "Chinese" is changed to "u4e2du6587" by default, it is not intuitive * If there is no special requirement, it is not recommended to use Json_encode, it is better to use it directly, save resources * JSON_ENCODE () parameter encoding format is UTF-8 to work properly * * @param array|object $data * @return Array|object */ function Ch_json_encode ($data) { $ret = Wphp_urlencode ($data); $ret = Json_encode ($ret); Return UrlDecode ($ret); } |
http://www.bkjia.com/PHPjc/632104.html www.bkjia.com true http://www.bkjia.com/PHPjc/632104.html techarticle in PHP json_encode Chinese display problem is a difficult problem for many programmers, the following I will give you two Chinese display problems in the solution, we can refer to. JSON has become ...