php5.2 's new JSON feature is very popular, but it's been tested that
Json_encode's handling of Chinese is problematic,
1. GB encoding cannot be processed, and all GB encodings are replaced with null characters.
2.UTF8 encoded in Chinese is encoded into Unicode encoding, equivalent to the result of JavaScript's escape function processing.
Copy Code code as follows:
/*
In order to use JSON correctly, we should first use the UTF8 code on the code and then the Json_ Encode's return results are handled with a little processing to get the right results.
I've written a simple class that wraps these two functions,
**/
class json{
public static function encode ($STR) {
$code = json_encod E ($STR);
Return Preg_replace ([0-9a-f]+) #ie, Iconv (' UCS-2 ', ' UTF-8 ', pack (' H4 ', ' \\1 ') ", $code);
}
public static function decode ($STR) {
return Json_decode ($STR);
}
//When used
Json::encode ($code);
Json::d ecode ($code);
/** This will correctly handle UTF8 encoded Chinese.
PS: For GB-encoded Chinese, we can change the encoding into UTF8 encoding, then encode, and then a UTF8-> GB conversion.
In addition to the general Json_encode results we are returned to the client to use, we can actually use the JavaScript unescape function to decode the Unicode encoded Chinese, so as to restore the correct Chinese.
or with: $title = mb_convert_encoding ($title, ' html-entities ', $this->_outcharset);//normal display
********/