//可转码 :字符串 和数组
//默认转化为 utf - 8
//转化utf - 8 to gbk 带参数 如:auto_charset($fContents, $to = 'GBK')
function auto_charset($fContents, $to = 'utf-8'){
if($to == ''){
$to = 'utf-8';
}
if(is_string($fContents)){
$from = mb_detect_encoding($fContents, array('UTF-8', 'GBK','GB2312', 'BIG5','EUC-CN','CP936'));//顺序很重要
if(in_array(strtoupper($from),array('ASCII','GB2312', 'GBK', 'BIG5','EUC-CN','CP936'),true)){
$from='GBK';
}
if(strtoupper($to) == strtoupper($from)){
return $fContents;
}
if(function_exists('mb_convert_encoding')){
return mb_convert_encoding($fContents, $to, $from);
}elseif(function_exists('iconv')){
return iconv($from, $to, $fContents);
}else{
return $fContents;
}
}elseif(is_array($fContents)){
foreach($fContents as $key => $val){
$_key = auto_charset($key, $to);
$fContents[$_key] = auto_charset($val, $to);
if($key != $_key){
unset($fContents[$key]);
}
}
return $fContents;
}else{
return $fContents;
}
}