Json_encode prevent Chinese characters from escaping into Unicode, json_encodeunicode_php tutorial

Source: Internet
Author: User

Json_encode prevents Chinese characters from escaping into Unicode, Json_encodeunicode

As you know, Json_encode usually escapes the characters in JSON into Unicode, but that's not necessarily what we want. Sometimes we need to get a JSON string in the form of a Chinese character, such as a JSON string that needs to be GBK encoded (as long as the character string is transcoded). Is there any good way?

PHP officially hears this demand and offers a reliable solution: Json_unescaped_unicode. This parameter ensures that Json_encode no longer converts Chinese characters to Unicode.

Does that seem to solve it? When we happily use this parameter, we find that there is no egg to use. In a closer look, this parameter is only supported by PHP after 5.4. What about the earlier PHP?

The community provides a solution:

function My_json_encode ($arr) {//convmap since x char codes so it takes all multibyte codes (above ASCII).  So such characters is being "hidden" from normal json_encodingarray_walk_recursive ($arr, function (& $item, $key) {if (Is_string ($item)) $item = Mb_encode_numericentity ($item, Array (x, XFFFF, xffff), ' utf-'); }); return Mb_decode_numericentity (Json_encode ($arr), array (x, XFFFF, xffff), ' utf-');}

However, this method is only supported in 5.3, since 5.2 does not support anonymous functions. As for the solution? Define the anonymous function as you please.

PS: Solve json_encode Chinese Unicode transcoding problem

When using PHP json_encode to handle Chinese, Chinese will be encoded, become unreadable, similar to the "\u***" format, if you want Chinese characters do not transcode, here are three ways

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);}

Articles you may be interested in:

    • PHP Json_encode Strange problem description
    • PHP Learning Random Notes _ Encoding (json_encode Chinese does not display)
    • How to use Json_decode () and Json_encode () in PHP
    • Summary of changes of Json_encode Chinese transcoding in PHP5.4
    • PHP json_encode values in curly braces and curly braces
    • PHP array Conversion JS array operation and Json_encode use of detailed
    • Analysis of PHP Json_encode () and Json_decode ()
    • The solution to the problem of Json_encode processing GBK and gb2312 Chinese characters in PHP
    • PHP does not escape the Chinese solution when using the Json_encode function

http://www.bkjia.com/PHPjc/1104338.html www.bkjia.com true http://www.bkjia.com/PHPjc/1104338.html techarticle Json_encode prevents Chinese characters from escaping into Unicode, json_encodeunicode everyone knows that Json_encode usually escapes the characters in Json into Unicode, but that's not necessarily what we want ...

  • Related Article

    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.