Introduction to how PHP implements json_decode without escaping Chinese methods

巴扎黑
Release: 2023-03-15 06:42:02
Original
2720 people have browsed it

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);
Copy after login

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');
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!