Json_encode is a problem in processing Chinese characters and cannot process GB encoding. all GB encoding will be replaced with null characters. The following is a good solution. for details, refer to JSON
The json function added by php5.2 is very popular, but it has been tested and found that,
Json_encode is problematic in processing Chinese characters,
1. cannot process the GB encoding. all the GB encoding will be replaced with null characters.
2. UTF-8 encoded Chinese characters are encoded as unicode, which is equivalent to the processing result of the javascript escape function.
The code is as follows:
/*
In order to use json correctly, we should first adopt utf8 encoding in encoding, and then slightly process the returned results of json_encode to get the correct results.
I wrote a simple class and wrapped the two functions,
**/
Class Json {
Public static function encode ($ str ){
$ Code = json_encode ($ str );
Return preg_replace ("# \\\ u ([0-9a-f] +) # ie", "iconv ('ucos-2', 'utf-8', pack ('h4 ', '\ 1') ", $ code );
}
Public static function decode ($ str ){
Return json_decode ($ str );
}
}
// When using
Json: encode ($ code );
Json: decode ($ code );
/** In this way, utf8 encoded Chinese characters can be correctly processed.
PS: For Chinese characters in GB encoding, we can convert them into UTF8 encoding before encoding. During decoding, we can convert them into utf8-> gb.
In addition, the result of json_encode is generally returned to the client for use. In fact, we can also use the javascript unescape function to decode the unicode-encoded Chinese characters to restore them to the correct Chinese characters.
Alternatively, use: $ title = mb_convert_encoding ($ title, 'HTML-ENTITIES ', $ this-> _ outCharset). // The data is normally displayed under any encoding.
********/