Problem solving of Json_encode Chinese Unicode transcoding in PHP

Source: Internet
Author: User
Tags urlencode

We introduced in the previous article The Json_encode () function in PHP and how to use it, when we work will use Json_encode to deal with Chinese, Chinese will be encoded, become unreadable, similar to "\u***" format, if you want Chinese characters do not transcode , here are three ways to solve the problem of Json_encode Chinese Unicode transcoding!

1. Upgrade PHP, in PHP5.4, the problem is finally resolved, JSON added an option: Json_unescaped_unicode, so the name Incredibles, that is, JSON do not encode UNICODE.

<?phpecho Json_encode ("Chinese", json_unescaped_unicode);//"Chinese"

2. UrlEncode the Chinese characters first and then use the Json_encode,json_encode again after using the UrlDecode to decode, so that the encoded JSON array of Chinese characters will not appear Unicode encoding.

$array = Array (' Test ' =>urlencode ("I Am Test"), $array = Json_encode ($array); Echo UrlDecode ($array);//{"test": "I am a Test"}

3. Decode the Unicode code and decode the function 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));

Summarize:

This article through three kinds of methods to solve the PHP json_encode Chinese Unicode transcoding problem, I believe that the small partners encountered the same problem can be easily solved!

Related recommendations;

Examples of using Json_encode in PHP


A detailed explanation of the Json_encode () function in PHP


Some descriptions of the Json_encode () and Json_decode () functions in PHP

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.