Home  >  Article  >  Backend Development  >  What should I do if Android gives php Chinese garbled characters?

What should I do if Android gives php Chinese garbled characters?

藏色散人
藏色散人Original
2022-10-24 09:33:531656browse

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.

What should I do if Android gives php Chinese garbled characters?

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( &#39;Name&#39;=>&#39;希亚&#39;,&#39;Age&#39;=>20);
$jsonencode = json_encode($arr);
echo $jsonencode;
?>

The program running results are as follows:

What should I do if Android gives php Chinese garbled characters?

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(&#39;possible deep recursion attack&#39;);    
}    
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, &#39;urlencode&#39;, true); 
$json = json_encode($array);
 return urldecode($json); 
} 
 $array = array ( &#39;Name&#39;=>&#39;希亚&#39;,&#39;Age&#39;=>20); 
 echo JSON($array);
?>

This time the code ran successfully

What should I do if Android gives php Chinese garbled characters?

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!

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