When you do Json_encode in php5.2. Chinese will be Unicode encoded,
php5.3 added the options parameter,
After 5.4 Join Json_unescaped_unicode, this parameter, do not need to do escape and UNICODE processing.
So we need to do a deal on Chinese before 5.4.
5.4 The inside of the treatment
PHP code
- Json_encode ( $str, Json_unescaped_unicode);
Before 5.4, there are two ways to handle
Method 1.
PHP code
- function Encode_json ($str) {
- $code = Json_encode ($str);
- return Preg_replace ("#\\\u ([0-9a-f]+) #ie", "iconv (' UCS-2 ', ' UTF-8 ', pack (' H4 ', ' \\1 '))", $code);
- }
Method 1. In practical application there is a problem, some characters will be dropped, more than why, such as String: "Date 11.2" will be changed to "date." 2 "
Method 2.
First to deal with the need to do urlencode processing, and then Json_encode, and finally do urldecode processing
PHP code
- function Encode_json ($str) {
- return UrlDecode (Json_encode (Url_encode ($str)));
- }
- /**
- *
- */
- function Url_encode ($str) {
- if (is_array ($str)) {
- foreach ($str as $key = =$value) {
- $str [UrlEncode ($key)] = Url_encode ($value);
- }
- } Else {
- $str = UrlEncode ($str);
- }
- return $str;
- }
PHP json_encode Chinese is transcoded problem