PHP5.4中json_encode中文转码的变化[让json更懂中文]
在php5.4以前做json_encode的时候中文会被unicode编码,中文都会被编码, 变成不可读的, 类似“\u***”的格式, 还会在一定程度上增加传输的数据量。
例如:
$str = '中文';
echo json_encode($str);
//"\u4e2d\u6587"
php5.4开始
echo json_encode($str, JSON_UNESCAPED_UNICODE);
//"中文"
php5.4让json更懂中文!
5.4之前不进行unicode转码,有3种方法处理:
PHP5.4中json_encode中文转码的变化让json更懂中文
但其实前两种方式是会导致问题,在一些特殊的情况下。如下例:
function myjson($code) {
$code = json_encode(urlencodeAry($code));
return urldecode($code);
}
function urlencodeAry($data) {
if(is_array($data)) {
foreach($data as $key=>$val) {
$data[$key] = urlencodeAry($val);
}
return $data;
} else {
return urlencode($data);
}
}
$test = array (
0 => '"大连周水子机场"→人民路',
1 => '运营时间:5:10~21:00 票价:16元 发车间隔20分钟一班,客满随时发车',
);
/*
通过常规则的json_encode|json_decode来编解码
["\"\u5927\u8fde\u5468\u6c34\u5b50\u673a\u573a\"\u2192\u4eba\u6c11\u8def","\u8fd0\u8425\u65f6
\u95f4\uff1a5\uff1a10\uff5e21\uff1a00 \u7968\u4ef7\uff1a16\u5143 \u53d1\u8f66\u95f4\u969420
\u5206\u949f\u4e00\u73ed\uff0c\u5ba2\u6ee1\u968f\u65f6\u53d1\u8f66"]
array (
0 => '"大连周水子机场"→人民路',
1 => '运营时间:5:10~21:00 票价:16元 发车间隔20分钟一班,客满随时发车',
)
*/
$test1 = json_encode($test);
$test2 = json_decode($test1, TRUE);
echo $test1;
echo PHP_EOL;
var_export($test2);
echo PHP_EOL;
/*
通过myjson|json_decode来编解码,但是会出报错json_last_error返回(JSON_ERROR_SYNTAX === 4),
因为""大连周水子机场"→人民路"
[""大连周水子机场"→人民路","运营时间:5:10~21:00 票价:16元 发车间隔20分钟一班,客满随时发车"]
NULL
*/
$test1_1 = myjson($test);
$test2_1 = json_decode($test1_1, TRUE);
echo $test1_1;
echo PHP_EOL;
var_export($test2_1);
echo PHP_EOL;
/*
通过json_enco+pack|json_decode来编解码,保证不会对中文编码,但是会缺少运营时间数据和票价
["\"大连周水子机场\"→人民路","运营时间::~: 票价:元 发车间隔分钟一班,客满随时发车"]
array ( www.2cto.com
0 => '"大连周水子机场"→人民路',
1 => '运营时间::~: 票价:元 发车间隔分钟一班,客满随时发车',
)
*/
function replaceUni($str) {
return preg_replace("#\\\u([0-9a-f]+)#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", $str);
}
$test1_2 = replaceUni(json_encode($test));
$test2_2 = json_decode($test1_2, TRUE);
echo $test1_2;
echo PHP_EOL;
var_export($test2_2);
echo PHP_EOL;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Call of Duty Warzone is a newly launched mobile game. Many players are very curious about how to set the language of this game to Chinese. In fact, it is very simple. Players only need to download the Chinese language pack, and then You can modify it after using it. The detailed content can be learned in this Chinese setting method introduction. Let us take a look together. How to set the Chinese language for the mobile game Call of Duty: Warzone 1. First enter the game and click the settings icon in the upper right corner of the interface. 2. In the menu bar that appears, find the [Download] option and click it. 3. Select [SIMPLIFIEDCHINESE] (Simplified Chinese) on this page to download the Simplified Chinese installation package. 4. Return to the settings

Excel spreadsheet is one of the office software that many people are using now. Some users, because their computer is Win11 system, so the English interface is displayed. They want to switch to the Chinese interface, but they don’t know how to operate it. To solve this problem, this issue The editor is here to answer the questions for all users. Let’s take a look at the content shared in today’s software tutorial. Tutorial for switching Excel to Chinese: 1. Enter the software and click the "File" option on the left side of the toolbar at the top of the page. 2. Select "options" from the options given below. 3. After entering the new interface, click the “language” option on the left

"WWE2K24" is a racing sports game created by Visual Concepts and was officially released on March 9, 2024. This game has been highly praised, and many players are eagerly interested in whether it will have a Chinese version. Unfortunately, so far, "WWE2K24" has not yet launched a Chinese language version. Will wwe2k24 be in Chinese? Answer: Chinese is not currently supported. The standard version of WWE2K24 in the Steam Chinese region is priced at 199 yuan, the deluxe version is 329 yuan, and the commemorative edition is 395 yuan. The game has relatively high configuration requirements, and there are certain standards in terms of processor, graphics card, or running memory. Official recommended configuration and minimum configuration introduction:

VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

Quark browser software provides countless resource information, and the search results are the most accurate. It has a powerful built-in search engine. You can find the corresponding answers by entering keywords. The overall Internet experience is very good. Browsing sections in different fields are open. Search for whatever you search and see. You can find it immediately. All annoying advertising pop-ups are blocked. All annoying advertising pop-ups are blocked. You can freely switch between different browsing modes and surf the Internet. There is no lag at all in the process, and of course the translation function can be turned on. Languages of any country can be easily translated, and there are no problems in communication. Files and documents compatible with various formats can be opened and viewed. Now the editor is browsing for Quark online in detail. Server users bring the method of setting Chinese translation. 1. First click on the mobile phone desktop

Tips for solving Chinese garbled characters written by PHP into txt files. With the rapid development of the Internet, PHP, as a widely used programming language, is used by more and more developers. In PHP development, it is often necessary to read and write text files, including txt files that write Chinese content. However, due to encoding format problems, sometimes the written Chinese will appear garbled. This article will introduce some techniques to solve the problem of Chinese garbled characters written into txt files by PHP, and provide specific code examples. Problem analysis in PHP, text

How to change Chinese to English in Google Chrome? Some friends want to set Google Chrome to English so that they can continuously improve their English during use. So how to set it to English? Google Chrome is Chinese by default. Below, I will show you how to set the language of Google Chrome to English. Let’s take a look. Setting steps: 1. Open [Google Chrome], as shown in the figure below. 2. Click the [three dots] menu in the upper right corner of the Google Chrome interface, as shown in the figure below. 3. After entering the menu page, find [Settings], as shown in the figure below. 4. After entering the settings page, click the [Language] option, as shown in the figure below. 5. Select [Add Language] in the language interface, as shown in the figure below.
