PHP Json_decode, cannot be converted into Chinese

Source: Internet
Author: User
Tags url decode
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.