How PHP implements Json_decode does not escape Chinese method introduction

Source: Internet
Author: User
This article mainly introduces the PHP implementation Json_decode does not escape the Chinese method, combined with the case-specific analysis of the php5.4+ and 5.3 version of the implementation of the Json_decode to achieve non-escaping Chinese specific operating skills and related considerations, the need for friends can refer to the following

In this paper, we describe a method that PHP implements Json_decode does not escape Chinese. Share to everyone for your reference, as follows:

By default, PHP's Json_decode method escapes special characters and also turns Chinese into Unicode encoded form.

This makes it cumbersome to view the text in a database. So we need to limit the escape to Chinese.

For the php5.4+ version, the Json_decode function is the second parameter that can be used to limit the escape range.

To restrict Chinese, use JSON_UNESCAPED_UNICODE parameters.

Json_encode ($a, json_unescaped_unicode);

For the PHP5.3 version, you can first convert more than ASCII 127 characters to HTML values to avoid being transcoded by the Json_decode function:

function My_json_encode ($arr) {//convmap since 0x80 char codes so it takes all mul Tibyte codes (above ASCII 127). So such characters is being "hidden" from normal json_encoding array_walk_recursive ($arr, function (& $item, $key)    {if (is_string ($item)) $item = Mb_encode_numericentity ($item, Array (0x80, 0xFFFF, 0, 0xFFFF), ' UTF-8 ');}); Return mb_decode_numericentity (Json_encode ($arr), Array (0x80, 0xFFFF, 0, 0xFFFF), ' UTF-8 ');} 

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.