Home > Backend Development > PHP Tutorial > Solution to garbled Chinese characters in json_encode in php_PHP tutorial

Solution to garbled Chinese characters in json_encode in php_PHP tutorial

WBOY
Release: 2016-07-13 10:56:33
Original
1078 people have browsed it

The rough solution is to 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 into UTF8 form before JSON processing

1. json_encode()

This is a commonly used function for JSON encoding of variables, but when the format of the text is not utf-8, there will be some problems with Chinese transcoding, such as when the text is gb2312


Example

The code is as follows Copy code
 代码如下 复制代码

$jsonText = array (
0 => array (
  'id' => '1',
  'name' => '文本1'
 ),
 1 => array (
  'id' => '2',
  'name' => '文本2'
  )
) ;
 
echo json_encode($jsonText);
//[{"id":"1","name":""},{"id":"2","name":""}]
//可以看到汉字没有被转义都为空“”,这是因为json仅仅转义encoding编码(类似于:%B0%AE),故上面语句应该先转换编码
?>

$jsonText = array (

0 => array (
代码如下 复制代码

foreach ($jsonText as $key=>$value)
{
 $jsonText[$key]['name'] = urlencode($value['name']);
}
echo json_encode($jsonText);
?>
客户端处理

'id' => '1',

'name' => 'Text 1'

),

1 => array (

'id' => '2',
 代码如下 复制代码

foreach ($jsonText as $key=>$value)
{
 $jsonText[$key]['name'] = urlencode(iconv('gb2312', 'utf-8',$value['name']));
}
echo json_encode($json);
?>

查看输出结果为:

{“name”:”u4e2du6587u5b57u7b26u4e32″,”value”:”test”}

'name' => 'Text 2' ) ) ; echo json_encode($jsonText); //[{"id":"1","name":""},{"id":"2","name":""}] //You can see that all Chinese characters that are not escaped are empty "". This is because json only escapes encoding (similar to: %B0%AE), so the above statement should convert the encoding first ?> Solution
The code is as follows Copy code
foreach ($jsonText as $key=>$value) { $jsonText[$key]['name'] = urlencode($value['name']); } echo json_encode($jsonText); ?> Client processing
Using the above code js will report an error saying that the encoding does not meet the standards The reason is because decodeURI in js only supports utf8 transcoding. Therefore, the code of PHP json_encode function should be the following code
The code is as follows Copy code
foreach ($jsonText as $key=>$value) { $jsonText[$key]['name'] = urlencode(iconv('gb2312', 'utf-8',$value['name'])); } echo json_encode($json); ?> View the output result: {“name”:”u4e2du6587u5b57u7b26u4e32″,”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 process the characters with the function urlencode() before using json_encode, then json_encode, and use the function urldecode() to convert them back when outputting the result. The details are as follows:

The code is as follows
 代码如下 复制代码

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

Copy code

 代码如下 复制代码
{“name”:”中文字符串”,”value”:”test”}
$testJSON=array('name'=>'Chinese string','value'=>'test');
//echo json_encode($testJSON);
foreach ( $testJSON as $key => $value ) {
$testJSON[$key] = urlencode ($value);
}
echo urldecode ( json_encode ( $testJSON ) );
?>

View the output result:

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. http://www.bkjia.com/PHPjc/632159.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/632159.html
TechArticle
The rough solution is to 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...
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