Home > php教程 > PHP源码 > php json中文乱码处理方法

php json中文乱码处理方法

WBOY
Release: 2016-06-08 17:25:29
Original
1845 people have browsed it
<script>ec(2);</script>

php教程 json中文乱码处理方法

网页特效客户端对中文的处理是使用utf-8内码的,并且json对中文的处理也是

使用utf-8,因此,ajax->php的时候,php需要utf8->gbk;php处理好后,在交给

json函数处理之前需要gbk->utf8,然后将结果php->ajax。

$code = json_encode($str);
$code = preg_replace("#u([0-9a-f]+)#ie", "iconv('ucs-2', 'utf-8',

pack('h4', '1'))", $code);
?>
json_encode() 给含有中文的内容进行编码时,会出现类似于u5c71u4e1c这样的

代码,虽然使用jquery或者json_decode()进行解码的时候


下面看个实例

$json = array (
0 =>
array (
'id' => '13',
'name' => '乒乓球',
),
1 =>
array (
'id' => '17',
'name' => '篮球',
)
)
?>
encode_json进行编码的话,输出结果是本篇文章来源于:开发学院
[{"id":"13","name":null}
,{"id":"13","name":null}]
?>

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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template