$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文件中文亂碼的終極解決方法[朗讀]
加入收藏