Detailed explanation of PHP json_encode() function and Chinese garbled code problem

高洛峰
Release: 2023-03-04 11:26:01
Original
3243 people have browsed it

Using the json_encode() built-in function in php (php > 5.2) can be used so that data in php can be transferred and used well with other languages.

The function of this function is to convert numerical values into json data storage format.

'希亚', 'Age'=> ); $jsonencode = json_encode($arr); echo $jsonencode; ?>
Copy after login

The program running result is as follows:

{"Name":null,"Age":}

json_encode function Chinese is encoded as null. I googled it and found that it is very simple. In order to closely integrate with the front-end, Json only supports utf- encoding. I think it is because the front-end Javascript is also utf-.

iconv('gb','utf-','这里是中文标题'), 'body'=>'abcd...' ); echo json_encode($array); ?>
Copy after login

The running result of this program is:

{"title":"\u8fd9\u91cc\u662f\u4e2d\u6587\u6807 \u9898","body":"abcd..."}

All Chinese characters in the array are missing or \u2353 etc. appear after json_encode.

The solution is to use the urlencode() function to process the following. Before json_encode, use urlencode() to process all the contents of all arrays, then use json_encode() to convert it into a json string, and finally use urldecode () Convert the encoded Chinese back.

 ) { die('possible deep recursion attack'); } foreach ($array as $key => $value) { if (is_array($value)) { arrayRecursive($array[$key], $function, $apply_to_keys_also); } else { $array[$key] = $function($value); } if ($apply_to_keys_also && is_string($key)) { $new_key = $function($key); if ($new_key != $key) { $array[$new_key] = $array[$key]; unset($array[$key]); } } } $recursive_counter--; } /************************************************************** * * 将数组转换为JSON字符串(兼容中文) * @param array $array 要转换的数组 * @return string 转换得到的json字符串 * @access public * *************************************************************/ function JSON($array) { arrayRecursive($array, 'urlencode', true); $json = json_encode($array); return urldecode($json); } $array = array ( 'Name'=>'希亚', 'Age'=> ); echo JSON($array); ?>
Copy after login

It was successful this time, the running results are as follows:

{"Name":"Xia","Age":"20 "}

The following will introduce to you the solution to Chinese garbled characters in PHP json_encode

I believe that many people have encountered the problem of Chinese garbled characters when using Ajax to interact with the background php page. As a lightweight data exchange format, JSON is very popular. However, using PHP as the background interaction is prone to the problem of Chinese garbled characters. JSON is the same as js. The characters on the client are processed in the form of UTF8. That is to say, when using JSON as the data format for submission and reception, the characters are processed by UTF8 encoding. When our page encoding and database encoding are not When using UTF8, it is extremely easy for Chinese garbled characters to appear. The solution is naturally to use UTF8 when processing JSON data with js or PHP.

PHP5.2 or above uses json_encode as a built-in function, which brings great convenience to website makers. However, we must note that json_encode only supports UTF8-encoded characters. Otherwise, Chinese characters will be garbled. Or a null value appears.

The solution is divided into the following two steps.

Step1

Ensure that characters are encoded in UTF8 when processing JSON. Specifically, we can change both the database encoding and page encoding to UTF8. Of course, if you like to use gbk encoding, you can convert the characters to UTF8 before JSON processing. There are the following methods in PHP:

Copy after login

Step2

Backend PHP page (the page is encoded as UTF-8 or the characters have been converted to UTF -8) Use json_encode to convert the array array in PHP into a JSON string. For example:

'中文字符串','value'=>'test'); echo json_encode($testJSON); ?>
Copy after login

The output result is:

{"name":"\u4e2d\u6587\u5b57\u7b26\u4e32″, "value":"test"}

It can be seen that even if UTF8-encoded characters are used, Chinese garbled characters appear when using json_encode. The solution is to use the urlencode() function to process the characters before using json_encode, then json_encode them, and use the urldecode() function to convert them back when outputting the results. The details are as follows:

'中文字符串','value'=>'test'); //echo json_encode($testJSON); foreach ( $testJSON as $key => $value ) { $testJSON[$key] = urlencode ( $value ); } echo urldecode ( json_encode ( $testJSON ) ); ?>
Copy after login

Check the output result:

{"name":"Chinese string","value":"test ”}

At this point, Chinese characters have been successfully output. Feel free to use json_encode. In this way, the JSON string output in the PHP background will not appear Chinese garbled when eval is received by Ajax in the front-end javascript, because js also processes JSON format data in the form of UTF8, which is similar to PHP, so it receives the PHP page The JSON string does not cause problems.


For more detailed explanations of the PHP json_encode() function and articles related to Chinese garbled characters, please pay attention to 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
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!