Php solves json

Source: Internet
Author: User
When PHP's json_encode is used to process Chinese characters, Chinese characters are encoded and become unreadable. If you want Chinese characters Not To Be transcoded, three methods are provided here. upgrade PHP. In PHP5.4, this problem was finally solved. Json adds an option: JSON_UNESCAPED_UNICODE. Therefore, the name of Json is invalid, that is, Json is not edited.

When PHP's json_encode is used to process Chinese characters, Chinese characters are encoded and become unreadable. If you want Chinese characters Not To Be transcoded, three methods are provided here. upgrade PHP. In PHP5.4, this problem was finally solved. Json adds an option: JSON_UNESCAPED_UNICODE. Therefore, the name of Json is invalid, that is, Json is not edited.

When PHP's json_encode is used to process Chinese characters, Chinese characters are encoded and become unreadable, similar to the "\ u ***" format. If you do not want to transcode Chinese characters, three methods are provided here.

1. Upgrade PHP. In PHP5.4, this problem was finally solved.Solution, Json adds an option: JSON_UNESCAPED_UNICODE, so it is named Unicode, that is, Json does not encode Unicode.

 

2. urlencode the Chinese characters first and then use json_encode. After json_encode, use urldecode again to decode them. In this way, the Encoded chinese characters in the json array will not undergo unicode encoding.

$ Array = array ('test' => urlencode ("I'm a test"); $ array = json_encode ($ array); echo urldecode ($ array ); // {"test": "I'm a test "}

3. decodes the unicode code. The decoding function is as follows:

Function decodeUnicode ($ str) {return preg_replace_callback ('// \\\ u ([0-9a-f] {4})/I', create_function ('$ matches ', 'Return mb_convert_encoding (pack ("H *", $ matches [1]), "UTF-8", "UCS-2BE"); '), $ str);} 4. example $ arr = array ('name1': "Chinese", 'name2': 'abc12 '); $ jsonstr = decodeUnicode (json_encode ($ arr ));

Http://www.cnblogs.com/sink_cup/archive/2011/05/28/php_json_encode_unicode_decode.html

Http://www.veryhuo.com/a/view/35112.html

Http://www.alixixi.com/program/a/2011112776664.shtml

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.