Php5.2Json cannot properly handle Chinese and GB encoding solutions. The json function added by php5.2 is very popular. However, tests have found that json_encode has a problem in Chinese processing. 1. unable to process the GB encoding, and all the GB encoding will be replaced with null php5.2. The new json function 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.
********/
Encoding, json_encode is problematic for Chinese characters. 1. unable to process the GB encoding, all the GB encoding will be replaced with null...