gbk下json_encode,中文返回null的问题
[ 2019-01-18 12:51:54 | 作者: admin ]
p.s. 还是可以先将gbk中文转utf8再使用json_encode函数
=========================================================================
参考:https://www.cnblogs.com/rwxwsblog/p/4731246.html
<?php
// json_encode() 保持中文方法详解
$arr['city'] = '北京';
$arr['name'] = 'weilong';
// 直接输出
// Res: {"city":"\u5317\u4eac","name":"weilong"}
echo json_encode($arr), "\n";
// 1. 加参数,PHP版本>5.4.0
// Res: {"city":"北京","name":"weilong"}
echo json_encode($arr, JSON_UNESCAPED_UNICODE), "\n"; // php > 5.4.0
// 2. 正则替换,json_encode后,正则将编码替换成中文
// Res: {"city":"北京","name":"weilong"}
echo preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", json_encode($arr)), "\n";
// 3. urldecode()、urlencode()函数,不推荐
// Res1: null, Res2: {"city":"北京","name":"weilong"}
echo urldecode(json_encode(urlencode($arr))), "\n";
$arr['city'] = urlencode($arr['city']); // urlencode()参数必须是string
echo urldecode(json_encode($arr)), "\n";
// 另外注意json_decode()参数区别。
$arr['city'] = '北京';
$arr['name'] = 'weilong';
$str = json_encode($arr);
$str2 = json_decode($str);
$str3 = json_decode($str, true);
print_r($str2); // object
/* Res:
stdClass Object
(
[city] => 北京
[name] => weilong
) */
print_r($str3); // array
/* Res:
Array
(
[city] => 北京
[name] => weilong
)
*/
?>
评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=2567
if($charset =="gbk"){
//gbk参数全部转utf8
$_POST = $this->array_iconv($_POST,'utf-8','gbk//IGNORE');
$_GET = $this->array_iconv($_GET,'utf-8','gbk//IGNORE');
//gbk数组转json
$data = array("city"=>"杭州");
$data = $this->array_iconv( $data ,'gbk','utf-8//IGNORE');
$result = json_encode( $data );
}
/**
* tianyang
* 字符串/二维数组/多维数组编码转换 ,递归方式转数据
* @param string $in_charset
* @param string $out_charset
* @param mixed $data
**/
function array_iconv($data, $in_charset='GBK', $out_charset='utf-8//IGNORE'){
if (!is_array($data)){
//$output = iconv($in_charset, $out_charset, $data);
if(is_string($data)){
$output = iconv($in_charset, $out_charset, $data);
}else{
$output = $data;
}
if(is_null($output))$output="";
}else{//判断是否数组
if(count($data)>0){
foreach($data as $key => $value){
$output[$key] = $this->array_iconv( $value,$in_charset ,$out_charset);
}
}else{
$output = array();
}
}
return $output;
}
//gbk参数全部转utf8
$_POST = $this->array_iconv($_POST,'utf-8','gbk//IGNORE');
$_GET = $this->array_iconv($_GET,'utf-8','gbk//IGNORE');
//gbk数组转json
$data = array("city"=>"杭州");
$data = $this->array_iconv( $data ,'gbk','utf-8//IGNORE');
$result = json_encode( $data );
}
/**
* tianyang
* 字符串/二维数组/多维数组编码转换 ,递归方式转数据
* @param string $in_charset
* @param string $out_charset
* @param mixed $data
**/
function array_iconv($data, $in_charset='GBK', $out_charset='utf-8//IGNORE'){
if (!is_array($data)){
//$output = iconv($in_charset, $out_charset, $data);
if(is_string($data)){
$output = iconv($in_charset, $out_charset, $data);
}else{
$output = $data;
}
if(is_null($output))$output="";
}else{//判断是否数组
if(count($data)>0){
foreach($data as $key => $value){
$output[$key] = $this->array_iconv( $value,$in_charset ,$out_charset);
}
}else{
$output = array();
}
}
return $output;
}
=========================================================================
参考:https://www.cnblogs.com/rwxwsblog/p/4731246.html
<?php
// json_encode() 保持中文方法详解
$arr['city'] = '北京';
$arr['name'] = 'weilong';
// 直接输出
// Res: {"city":"\u5317\u4eac","name":"weilong"}
echo json_encode($arr), "\n";
// 1. 加参数,PHP版本>5.4.0
// Res: {"city":"北京","name":"weilong"}
echo json_encode($arr, JSON_UNESCAPED_UNICODE), "\n"; // php > 5.4.0
// 2. 正则替换,json_encode后,正则将编码替换成中文
// Res: {"city":"北京","name":"weilong"}
echo preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", json_encode($arr)), "\n";
// 3. urldecode()、urlencode()函数,不推荐
// Res1: null, Res2: {"city":"北京","name":"weilong"}
echo urldecode(json_encode(urlencode($arr))), "\n";
$arr['city'] = urlencode($arr['city']); // urlencode()参数必须是string
echo urldecode(json_encode($arr)), "\n";
// 另外注意json_decode()参数区别。
$arr['city'] = '北京';
$arr['name'] = 'weilong';
$str = json_encode($arr);
$str2 = json_decode($str);
$str3 = json_decode($str, true);
print_r($str2); // object
/* Res:
stdClass Object
(
[city] => 北京
[name] => weilong
) */
print_r($str3); // array
/* Res:
Array
(
[city] => 北京
[name] => weilong
)
*/
?>
[最后修改由 admin, 于 2019-01-18 13:02:12]

这篇日志没有评论。
此日志不可发表评论。