看到你,梁松远的博客

位置 » 主页 » 代码分享 » php自动识别文本编码并转换成utf-8编码函数

php自动识别文本编码并转换成utf-8编码函数

 /**

* 编码转换
*/
function diconv($str,$inCharsets){
$charsetLists = array();
if ( !empty($inCharsets) && is_array($inCharsets) ){
foreach ( $inCharsets AS $_charset ){
if ( ($_charset = strtoupper($_charset)) && $_charset != 'UTF-8' && !isset($charsetLists[$_charset]) ){
$charsetLists[$_charset] = $_charset;
}
}
}
foreach ( array('GBK','GB2312','BIG5') AS $_charset ){
if ( !isset($charsetLists[$_charset]) ){
$charsetLists[$_charset] = $_charset;
}
}
 
$charset = 'UTF-8';
foreach ( $charsetLists AS $_charset ){
if ( $str === @iconv('UTF-8', $_charset, @iconv($_charset, 'UTF-8', $str)) ){
$charset = $_charset;
break;
}
}
return array(
$charset,
$charset != 'UTF-8' ? @iconv($charset,'UTF-8//IGNORE',$str) : $str,
);
}