Json_encode prevents conversion of Chinese characters to unicode. json_encodeunicode

Source: Internet
Author: User

Json_encode prevents conversion of Chinese characters to unicode. json_encodeunicode

As we all know, json_encode usually converts Chinese Characters in json to unicode, but this is not necessarily what we want. Sometimes, we need to obtain a json string in the Chinese character format, such as a json string encoded in gbk format (you only need to transcode the string in the Chinese character format ). Is there any good way?

Php officially heard this requirement and provided a reliable solution: JSON_UNESCAPED_UNICODE. This parameter ensures that json_encode no longer converts Chinese characters to unicode.

Does this seem to be solved? When we happily use this parameter, we find that there is no such thing. This parameter is only supported by php after 5.4. What should we do in the early stage of 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 are 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 by 5.3, because 5.2 does not support anonymous functions. As for the solution? Define an anonymous function.

Ps: Solve the Problem of json_encode Chinese UNICODE Transcoding

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. Json added an option: JSON_UNESCAPED_UNICODE, so the name is Unicode, that is, Json does not encode Unicode.

<? Phpecho json_encode ("Chinese", JSON_UNESCAPED_UNICODE); // "Chinese"

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

Articles you may be interested in:
  • Php json_encode
  • PHP learning note _ encoding (json_encode is not displayed in Chinese)
  • How to Use json_decode () and json_encode () in php
  • Summary of changes in json_encode Chinese transcoding in PHP5.4
  • Difference between braces in php json_encode values
  • Php array conversion js Array Operations and usage of json_encode
  • Analysis of json_encode () and json_decode () in php ()
  • How to solve the garbled problem between gbk and gb2312 in php json_encode
  • PHP does not escape Chinese characters when using the json_encode Function

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.