用 mb_convert_encoding 大多不能得到正确的判断
所以 mb_string 扩展有了一个新成员 mb_check_encoding
这个函数可以准确的判定来源是否为指定的字符集
只不过你需要写代码来枚举可能的字符集:
if(mb_check_encoding($s, 'utf-8')) ....
elseif(mb_check_encoding($s, 'gbk')) ....
elseif(mb_check_encoding($s, 'big5')) ....
else ....
所以 mb_string 扩展有了一个新成员 mb_check_encoding
这个函数可以准确的判定来源是否为指定的字符集
只不过你需要写代码来枚举可能的字符集:
if(mb_check_encoding($s, 'utf-8')) ....
elseif(mb_check_encoding($s, 'gbk')) ....
elseif(mb_check_encoding($s, 'big5')) ....
else ....
mb_convert_encoding($file_content,'UTF-8',"ASCII");
}elseif(mb_convert_encoding($file_content,'GB2312')){
mb_convert_encoding($file_content,'UTF-8',"GB2312");
}elseif(mb_convert_encoding($file_content,'GBK')){
mb_convert_encoding($file_content,'UTF-8',"GBK");
}elseif(mb_convert_encoding($file_content,'BIG5')){
mb_convert_encoding($file_content,'UTF-8',"BIG5");
}else{
mb_convert_encoding($file_content,'UTF-8',"auto");
}
还有有乱码
http://fashionistess.com/awr1/1.php
$charset = '';
if(mb_check_encoding($file_content, 'utf-8')) $charset = ''; //已是 utf-8 了无需转码
elseif(mb_check_encoding($file_content, 'big5')) $charset = 'big5'; //big5 的码值范围小于 gbk,应先判断
elseif(mb_check_encoding($file_content, 'gbk')) $charset = 'gbk'; //gb2312 是 gbk 的子集,只判 gbk 就可以了
if($charset)
$file_content = mb_convert_encoding($file_content, 'utf-8', $charset);
链接是测试结果 编码问题会影响解析的结果(以前只分析linux下生成的文件所以一直没发现这个问题) 谢谢 问题已经解决啦