$line =file_get_contents($file);
$first2 = substr($line,0,2);
$first3 = substr($line,0,3);
$first4 = substr($line,0,3);
$encodtype = "";
if ($first3 == utf8_bom)
$encodtype = 'utf-8 bom';
else if ($first4 == utf32_big_endian_bom)
$encodtype = 'utf-32be';
else if ($first4 == utf32_little_endian_bom)
$encodtype = 'utf-32le';
else if ($first2 == utf16_big_endian_bom)
$encodtype = 'utf-16be';
else if ($first2 == utf16_little_endian_bom)
$encodtype = 'utf-16le';
if ($encodtype!= '') {//判断是否为utf-16及utf-32。
if($encodtype!='utf-8 bom') $line = iconv($encodtype,"utf-8",$line);
} else {
if(!$mb2en=mb_detect_encoding($string,array("ascii","gb2312","gbk","big5",'cp936'))) echo '未知编码';
var_dump($mb2en);
$line=mb_convert_encoding($line,'utf-8',$mb2en);
}
- 编程问答
- 答案列表
PHP 读取txt文件中文乱码的终极解决方法[朗读]
加入收藏