When PHP outputs json data, an introduction to the method of not encoding unicode in Chinese

黄舟
Release: 2023-03-06 20:42:01
Original
1429 people have browsed it

Today when writing the API interface, I converted the PHP array into json, but there is a rule that unicode encoding cannot be used

I tried several methods:

If PHP is 5.4 or above You can directly use the JSON_UNESCAPED_UNICODE parameter

json_encode('Chinese test',JSON_UNESCAPED_UNICODE);

This method is feasible for strings, but not for arrays (to be verified)

2 . Urlencode the string in the array, then convert the array json_encode into json, and finally urldecode

Note: urlencode() and urldecode() convert Chinese characters to hexadecimal and proceed according to certain rules String combination, realizes character encoding and decoding, and ensures the integrity and compatibility of characters during URL data transmission

The above method does avoid unicode encoding for Chinese, but when I finally tested the interface, it still failed If it doesn’t pass, just use normal json_encode to avoid misleading others. I will continue to test in the future...

The above is the detailed content of When PHP outputs json data, an introduction to the method of not encoding unicode in Chinese. 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!