Home >Backend Development >PHP Problem >What should I do if Android gives php Chinese garbled characters?
Solution to android's Chinese garbled code: 1. Open the corresponding php file; 2. Before "json_encode", use "urlencode()" to process all the contents of all arrays; 3. Use " json_encode()" into a json string; 4. Use "urldecode()" to convert the encoded Chinese back.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
What should I do if Android gives php Chinese garbled characters?
1. Android itself cannot directly access the mysql database. You need to use PHP web pages to complete the operation of the mysql database. The exchange of data between PHP and Android requires the use of JSON format.
2. What is json
At the beginning of the 21st century, Douglas Crockford was looking for a simple data exchange format that could exchange data between servers. The common data exchange language at that time was XML, but Douglas Crockford felt that the generation and parsing of XML was too troublesome, so he proposed a simplified format, which is Json.
The specifications of Json are very simple and can be explained clearly in just a few hundred words on a page, and Douglas Crockford claims that this specification will never need to be upgraded because all the regulations are stipulated.
1) Parallel data are separated by commas (", ").
2) Mapping is represented by colon (": ").
3) The collection (array) of parallel data is represented by square brackets ("[]").
4) The mapped collection (object) is represented by curly brackets ("{}").
The above four rules are all the contents of Json format.
For example, the following sentence:
"Beijing has an area of 16,800 square kilometers and a permanent population of 16 million. Shanghai has an area of 6,400 square kilometers and a permanent population of 18 million.
It is written in json format like this:
[ {"城市":"北京","面积":16800,"人口":1600}, {"城市":"上海","面积":6400,"人口":1800}]
If the structure of the data is known in advance, the above writing method can be further simplified:
[ ["北京",16800,1600], ["上海",6400,1800]]
From this we can see that json is very easy to learn and easy to learn. Used. Therefore, in just a few years, it replaced xml and became the most popular data exchange format on the Internet.
3. JSON Chinese garbled code in PHP
In PHP Using the json_encode() built-in function (php > 5.2), data in php can be transferred and used well with other languages. The function of this function is to convert values into json data storage format.
<?php $arr = array( 'Name'=>'希亚','Age'=>20); $jsonencode = json_encode($arr); echo $jsonencode; ?>
The program running results are as follows:
The Chinese in the array appears with garbled characters such as \u5e0c\u4e9a after json_encode.
The solution is to use urlencode The () function handles 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() to convert the encoded Chinese back.
<?php /************************************************************** * * 使用特定function对数组中所有元素做处理 * @param string &$array 要处理的字符串 * @param string $function 要执行的函数 * @return boolean $apply_to_keys_also 是否也应用到key上 * @access public * *************************************************************/ function arrayRecursive(&$array, $function, $apply_to_keys_also = false) { static $recursive_counter = 0; if (++$recursive_counter > 1000) { 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'=>20); echo JSON($array); ?>
This time the code ran successfully
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What should I do if Android gives php Chinese garbled characters?. For more information, please follow other related articles on the PHP Chinese website!