PHP implements the json_decode method without escaping Chinese characters,

Source: Internet
Author: User

PHP implements the json_decode method without escaping Chinese characters,

This example describes how to implement json_decode without escaping Chinese Characters in PHP. We will share this with you for your reference. The details are as follows:

By default, the json_decode method of PHP will escape special characters and convert Chinese charactersUnicodeEncoding format.

This makes it very difficult for the database to view the text. Therefore, we need to limit the escape of Chinese characters.

For PHP5.4 +, the second parameter of the json_decode function can be used to limit the escape range.

To restrict Chinese characters, useJSON_UNESCAPED_UNICODEParameters.

json_encode($a, JSON_UNESCAPED_UNICODE);

For PHP5.3, You can first convert ASCII 127 or more 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 multibyte codes (above ASCII 127). So such characters are 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');}

PS: Here are some useful json online tools for your reference:

Online JSON code verification, validation, beautification, and formatting tools:
Http://tools.jb51.net/code/json

JSONOnline formatting tool:
Http://tools.jb51.net/code/jsonformat

Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson

JsonCode Online formatting/beautification/compression/editing/conversion tools:
Http://tools.jb51.net/code/jsoncodeformat

C language style/HTML/CSS/json code formatting and beautification tools:
Http://tools.jb51.net/code/ccode_html_css_json

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.