Home >Backend Development >PHP Tutorial >Introduction to how PHP implements json_decode without escaping Chinese methods

Introduction to how PHP implements json_decode without escaping Chinese methods

巴扎黑
巴扎黑Original
2018-05-22 09:50:272759browse

This article mainly introduces the method of PHP to implement json_decode without escaping Chinese, and combines the example form with a detailed analysis of the specific operating skills and related precautions for php5.4+ and 5.3 versions to implement json_decode without escaping Chinese. What is needed Friends can refer to

The example in this article describes how to implement json_decode without escaping Chinese in PHP. Share it with everyone for your reference, the details are as follows:

By default, PHP's json_decode method will escape special characters and convert Chinese into Unicode encoding form.

This makes viewing the text in the database cumbersome. So we need to limit the escaping of Chinese.

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

To limit Chinese, use the JSON_UNESCAPED_UNICODE parameter.

json_encode($a, JSON_UNESCAPED_UNICODE);

For PHP5.3 version, you can first convert characters above ASCII 127 into 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');
}

The above is the detailed content of Introduction to how PHP implements json_decode without escaping Chinese methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn