<返回更多

PHP 转码函数auto_charset:可对字符串和数组转码

2022-06-20    天目神泉
加入收藏

//可转码 :字符串 和数组

//默认转化为 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;

}

}

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>