Detailed explanation of how to implement json_decode without escaping Chinese in PHP

墨辰丷
Release: 2023-03-27 12:36:02
Original
2716 people have browsed it

This article mainly introduces the method of PHP to implement json_decode without escaping Chinese. It combines the example form with a detailed analysis of the specific operation skills and related precautions for php5.4 and 5.3 versions to implement json_decode without escaping Chinese. Friends who need it You can refer to

for details 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 the PHP5.3 version, you can first convert characters above ASCII 127 into HTML values ​​to avoid being converted by the json_decode function Code:

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

Related recommendations:

php implements the app interface and returns the json method of data

##getJSON() asynchronous request server Return json format data (graphic tutorial)

JSON object definition and value in JS Detailed explanation of implementation steps

##

The above is the detailed content of Detailed explanation of how to implement json_decode without escaping Chinese in PHP. 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