Previously encountered will JSON, json_decode after the appearance of Chinese garbled, but at least can be converted, this time unexpectedly can not be converted into Chinese,
Very depressed, show the following u9633u9633u4ecau5e74u5fc5u53d1u8d22, coding problems or ...
Reply content:
Previously encountered will JSON, json_decode after the appearance of Chinese garbled, but at least can be converted, this time unexpectedly can not be converted into Chinese,
Very depressed, show the following u9633u9633u4ecau5e74u5fc5u53d1u8d22, coding problems or ...
Alas, I despise myself, dear friends, when I forgot to escape
PHP version 5.4 or higher:
Json_encode ($data, Json_unescaped_unicode);
PHP version 5.2 and above have Json_encode/json_decode series functions.
PHP version 5.2-5.3, can be used by the combination of PHP urlencode/urldecode, can achieve similar effect of retaining Chinese.
Examples are: the Myjsonencode and Myjsondecode functions in the code can be reserved in Chinese.
The array conversion is reserved for the Chinese JSON string function Myjsonencode ($data) {return UrlDecode (Json_encode (Myurlencode ($data))); Requires PHP version 5.4 or more://return Json_encode ($data, Json_unescaped_unicode);} A JSON string that retains Chinese is converted to an array function Myjsondecode ($data) {$data = UrlEncode ($data); $data = Str_replace ("%7b", ' {', $data); $data = Str_replace ("%7d", '} ', $data); $data = Str_replace ("%5b", ' [', $data); $data = Str_replace ("%5d", '] ', $data); $data = Str_replace ("%3a", ': ', $data); $data = Str_replace ("%2c", ', ', $data); $data = Str_replace ("%22", ' "', $data); Return Myurldecode (Json_decode ($data, True));} The custom URL-encoded function Myurlencode ($data) {//Can be URL-encoded on an associative array and handle newline characters//internal recursive calls//for Myjsonencode function calls if (!is_array ($ Data) {$data = Str_replace ("\ r", ' \ R ', $data); $data = Str_replace ("\ n", ' \ n ', $data); $data = UrlEncode ($data); } else {foreach ($data as $key = + $value) {$data [Myurlencode ($key)] = Myurlencode ($value); if (string) Myurlencode ($key)!= = (string) $key) {unset ($data [$key]); }}} return $data;} Custom URL decoding function Myurldecode ($data) {//can URL decode an associative array and handle newline characters//internal recursive call//for Myjsondecode function call if (!is_array ($ Data) {$data = UrlDecode ($data); $data = Str_replace (' \ r ', "\ R", $data); $data = Str_replace (' \ n ', "\ n", $data); } else {foreach ($data as $key = + $value) {$data [Myurldecode ($key)] = Myurldecode ($value); if (string) Myurldecode ($key)!== (string) $key) {unset ($data [$key]); }}} return $data;}
The above code comes from the myphp Open Source Library.
GitHub Open Source Address: Https://github.com/MoonLord-LM/MyPHP
Brush a friend chain. My blog:http://www.moonlord.cn.