数组混合GBK、UTF-8 互相转换
程序员文章站
2022-05-25 11:23:33
...
/** * 数组混合GBK、UTF-8 换转为UTF-8 * @param unknown_type $arr */ static function gbktoutf8($arr){ if(is_array($arr) && count($arr)){ foreach($arr as $key=>$value){ if(is_array($value)){ $arrRs[$key] = gbktoutf8($value); }else{ if(!is_utf8($value)){ $arrRs[$key] = iconv('GBK','UTF-8',$value); }else{ $arrRs[$key] = $value; } } } return $arrRs; } return null; } /** * 数组混合GBK、UTF-8 换转为gbk * @param array $arr 目标数组 */ static function utf8togbk($arr){ if(is_array($arr) && count($arr)){ foreach($arr as $key=>$value){ if(is_array($value)){ $arrRs[$key] = utf8togbk($value); }else{ //判断字符编码是否utf8字符(如果不是utf8字符则转换) if(is_utf8($value)){ $arrRs[$key] = iconv('UTF-8','GBK',$value); }else{ $arrRs[$key] = $value; } } } return $arrRs; } return 0; }