function auto_charset($fcontents,$from='',$to='')
{
if( strtoupper($from) === strtoupper($to) || empty($fcontents) || (is_scalar($fcontents) && !is_string($fcontents)) ){
//如果编码相同或者非字符串标量则不转换
return $fcontents;
}
$from = strtoupper($from)=='utf8'? 'utf-8':$from;
$to = strtoupper($to)=='utf8'? 'utf-8':$to;
if(is_string($fcontents) ) {
if(function_exists('mb_convert_encoding')){
return mb_convert_encoding ($fcontents, $to, $from);
}elseif(function_exists('iconv')){
return iconv($from,$to,$fcontents);
}else{
exit('转换失败');
return $fcontents;
}
}
elseif(is_array($fcontents)){
foreach ( $fcontents as $key => $val ) {
$_key = auto_charset($key,$from,$to);
$fcontents[$_key] = auto_charset($val,$from,$to);
if($key != $_key ) {
unset($fcontents[$key]);
}
}
return $fcontents;
}
elseif(is_object($fcontents)) {
$vars = get_object_vars($fcontents);
foreach($vars as $key=>$val) {
$fcontents->$key = auto_charset($val,$from,$to);
}
return $fcontents;
}
else{
return $fcontents;
}
}
http://www.bkjia.com/phpjc/631381.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/631381.htmltecharticle编码转换 这段代码是thinkphp框架中的,感觉很常用,所以单独拎出来,大家共同学习一下。 function auto_charset($fcontents,$from='',$to='') { if( str...
