Home > Backend Development > PHP Tutorial > json_encode gbk/gb2312 Chinese garbled code in php_PHP tutorial

json_encode gbk/gb2312 Chinese garbled code in php_PHP tutorial

WBOY
Release: 2016-07-13 10:23:37
Original
984 people have browsed it

php中json_encode gbk/gb2312中文乱码

   1.json_encode()中文在gbk/gb2312中中文返回为null

 代码如下  

$arr = array ( 

 代码如下  

$arr = array ( 

  array ( 

      'catid' => '4', 

      'catname' => 'www.111cn.net', 

      'meta_title' => '一聚教程网2' 

    )
);
 
echo json_encode($arr);

结果

[{"catid":"4","catname":"www.111cn.net","meta_title":null}]

  array ( 

      'catid' => '4', 

      'catname' => 'www.111cn.net', 

 代码如下  
$data=”JSON中文”;
$newData=iconv(“GB2312″,”UTF-8//IGNORE”,$data);
echo $newData;
//ignore的意思是忽略转换时的错误,如果没有ignore参数,所有该字符后面的字符都不会被保存。
//或是(“GB2312″,”UTF-8″,$data);
?>

      'meta_title' => '一聚教程网2' 

    )
);
 
echo json_encode($arr);

结果

[{"catid":"4","catname":"www.111cn.net","meta_title":null}]

  看一了吗"meta_title":null 他本来是有一个值的为一聚教程网2了,这个我们查了一下原理是json_encode只支持uft-8编码,我们转换一下

Step2

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

 代码如下  
$testJSON=array(‘name’=>’中文字符串’,’value’=>’test’);
echo json_encode($testJSON);
?>
查看输出结果为:
{“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’=>’Chinese string’,’value’=>’test’);
//echo json_encode($testJSON);
foreach ( $testJSON as $key => $value ) {
$testJSON[$key] = urlencode ( $value );
}
echo urldecode ( json_encode ( $testJSON ) );
?>

 代码如下  

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

查看输出结果为:

{“name”:”中文字符串”,”value”:”test”}

View the output result:

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

Summary: The json_encode function can only process uft8 strings. If it is Chinese, it probably does not handle bytes well, because the lengths of Chinese gbk and uft are different. I will not introduce this in depth.

http://www.bkjia.com/PHPjc/834971.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/834971.htmlTechArticle
json_encode in php gbk/gb2312 Chinese garbled code 1.json_encode() Chinese in gbk/gb2312 Chinese return is null code As follows $arr = array ( array ( 'catid' = '4', 'catname' = 'www.111cn.net', 'm...
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template