PHP Json_decode stripcslashes

Source: Internet
Author: User
Tags url decode urlencode
echo $res=addslashes(json_encode(array('content'=>'中文')));//{\"content\":\"\\u4e2d\\u6587\"}echo stripcslashes($res);//{"content":"\u4e2d\u6587"}直接echo stripcslashes( '{\"content\":\"\\u4e2d\\u6587\"}');//{"content":"u4e2du6587"}\不见了???不能用json_decode()解码了

Reply content:

echo $res=addslashes(json_encode(array('content'=>'中文')));//{\"content\":\"\\u4e2d\\u6587\"}echo stripcslashes($res);//{"content":"\u4e2d\u6587"}直接echo stripcslashes( '{\"content\":\"\\u4e2d\\u6587\"}');//{"content":"u4e2du6587"}\不见了???不能用json_decode()解码了

JSON is escaped when encoded, addslashes and stripcslashes are superfluous.

PHP version 5.4 or higher:
Json_encode ($data, Json_unescaped_unicode);

The Json_encode/json_decode series function is available in

//php version 5.2.
//php version 5.2-5.3, you can use the combination of PHP urlencode/urldecode, you can achieve a similar effect of retaining Chinese. The
///example is as follows: 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
My blog:http://www.moonlord.cn.

A string direct write represents a normal escape. Say encode end of not can direct decode, why still do this extra action ...

  • 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.