Json has become the most common data format for web development. php also supports json and array conversion functions json_encode and json_decode from 52. However, we
Json has become the most common data format for web development. php also supports json and array conversion functions json_encode and json_decode from 5.2. However, we will find that (the Chinese character "you" is used as an example below) all Chinese characters converted using the json_encode function are encoded like u4f60 (you, although it does not affect program execution, it is not intuitive.
First, json_encode converts the processing of Chinese characters to the hexadecimal representation u4f60 of the corresponding unicode code (similar to the js escape function (% u4f60), that is, 0x4f60. Therefore, we only need to convert the unicode code (UCS-2) into UTF-8 encoded Chinese characters, the function is as follows:
- /**
- * Json_encode supports the Chinese version.
- * @ Param mixed $ data parameter and json_encode are identical
- */
- 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 * ',' $ 1'); ", $ data );
- }
Here, first convert the target data to a json string of the unicode encoding code, then use the regular expression to replace the four letters starting with the corresponding u with the corresponding text, and then transcode it again. The e in preg_replace regular expression allows the second parameter to perform the eval operation. it matches uxxxx first, and then converts the hexadecimal value xxxx to a Unicode character through the pack function, then convert the Unicode code to the UTF-8 code, and then you can see the normal Chinese characters.
The other json_encode () solution does not support Chinese characters. the code is as follows:
- /**
- * Urlencode the array and scalar
- * Generally, wphp_json_encode () is called ()
- * Handling the problem of json_encode Chinese display
- * @ 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); // recursively call this function
- } Else if (is_object ($ data )){
- $ Data-> $ k = wphp_urlencode ($ v );
- }
- }
- }
- Return $ data;
- }
- /**
- * Json encoding
- *
- * The problem that the Chinese characters are not displayed intuitively after json_encode () processing
- * By default, "Chinese" is changed to "u4e2du6587", which is not intuitive.
- * If you do not have special requirements, we do not recommend using this function. using json_encode directly is better, saving resources.
- * Json_encode () parameters can work properly only when the encoding format is UTF-8
- *
- * @ Param array | object $ data
- * @ Return array | object
- */
- Function ch_json_encode ($ data ){
- $ Ret = wphp_urlencode ($ data );
- $ Ret = json_encode ($ ret );
- Return urldecode ($ ret );
- }