Home >Backend Development >PHP Tutorial >Detailed explanation of how to implement json_decode without escaping Chinese in PHP

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

墨辰丷
墨辰丷Original
2018-05-22 14:30:492743browse

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);

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

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!

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