<?php
$array_test = array("status"=>0,"desc"=>iconv("gbk","utf-8","成功"),"title"=>iconv("gbk","utf-8","我的博客"),"blogId"=>"100");//将中文的gbk转为utf-8,便于json_encode编码,否则中文为空:gbk的PHP文件必须转为utf-8的编码。
$result = json_encode($array_test);
print_r($result);
$json2array = json_decode($result,True);//True让它转为数组,这里特别注意!
$json2array['desc'] = iconv("utf-8","gbk",$json2array['desc']);//将utf-8---->gbk2312 浏览器用Charset:GBK2312显示才能正常
$json2array['title'] = iconv("utf-8","gbk",$json2array['title']);//同上
//去掉上面两行,浏览器在Utf-8下面显示中文正常,Charset:GBK2312下不正常,乱码!
print_r($json2array);
?>
$array_test = array("status"=>0,"desc"=>iconv("gbk","utf-8","成功"),"title"=>iconv("gbk","utf-8","我的博客"),"blogId"=>"100");//将中文的gbk转为utf-8,便于json_encode编码,否则中文为空:gbk的PHP文件必须转为utf-8的编码。
$result = json_encode($array_test);
print_r($result);
$json2array = json_decode($result,True);//True让它转为数组,这里特别注意!
$json2array['desc'] = iconv("utf-8","gbk",$json2array['desc']);//将utf-8---->gbk2312 浏览器用Charset:GBK2312显示才能正常
$json2array['title'] = iconv("utf-8","gbk",$json2array['title']);//同上
//去掉上面两行,浏览器在Utf-8下面显示中文正常,Charset:GBK2312下不正常,乱码!
print_r($json2array);
?>
字符转化的函数:
来自PHP高级编程QQ群:我姓--]苏
function zhjson($v){
if(is_array($v)){
foreach($v as $key =>$value){
$v[$key]=zhjson($value);
}
return $v;
}else{
return iconv("gb2312","utf-8",$v);
}
}
if(is_array($v)){
foreach($v as $key =>$value){
$v[$key]=zhjson($value);
}
return $v;
}else{
return iconv("gb2312","utf-8",$v);
}
}
让我想起我们自己的一个函数:(一样一样的)
function addslashes_array(&$ar)
{
if (is_array ( $ar ))
{
foreach ( $ar as $key => $ar_sub )
{
$this->addslashes_array ( $ar [$key] );
}
}
else if (is_string ( $ar ))
{
$ar = addslashes ( $ar );
}
}
{
if (is_array ( $ar ))
{
foreach ( $ar as $key => $ar_sub )
{
$this->addslashes_array ( $ar [$key] );
}
}
else if (is_string ( $ar ))
{
$ar = addslashes ( $ar );
}
}
这个方法也还不错:
膘叔(19129540) 15:27:14
来自PHP高级编程QQ群
乱码我更不搞了,我会直接在smarty模版里调用mb_string
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1672/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2009-3-26 15:43
评论列表