由於業務上的需求,須要使用PHP讀取一個TXT文件,可是在設計到中文的問題上,就遇到了噁心的亂碼問題;web
首先查看一下TXT的編碼格式有四種:ANSI、Unicode、Unicode Big Endian、UTF-8編碼
一、先是使用mb_detect_encoding($contents, array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII'))語句spa
發現即便在其中增長了Unicode格式,已經沒法得到文件的編碼格式,可是對與ANSI和UTF-8格式卻是能夠使用;設計
二、因而針對這個問題,專門作了一個以下的轉換:code
$str = mb_convert_encoding ( $str, 'UTF-8','Unicode');orm
只是須要在前面加上一個編碼格式的判斷。ci
完整代碼以下:get
if ($fname = $_FILES['nickname']['tmp_name']) {it
//獲取文件的編碼方式class
$contents = file_get_contents($fname);
$encoding = mb_detect_encoding($contents, array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII'));
$fp=fopen($fname,"r");//以只讀的方式打開文件
$text = "";
$num = 0;
if(!(feof($fp))) {
$num++;
$str = trim(fgets($fp));
if ($encoding != false) {
$str = iconv($encoding, 'UTF-8', $str);
if ($str != "" and $str != NULL) {
$text = $str;
}
}
else {
$str = mb_convert_encoding ( $str, 'UTF-8','Unicode');
if ($str != "" and $str != NULL) {
$text = $str;
}
}
}
while(!(feof($fp))) {
$str = '';
$str = trim(fgets($fp));
if ($encoding != false) {
$str = iconv($encoding, 'UTF-8', $str);
if ($str != "" and $str != NULL) {
$text = $text.",".$str;
}
}
else {
$str = mb_convert_encoding ( $str, 'UTF-8','Unicode');
if ($str != "" and $str != NULL) {
$text = $text.",".$str;
}
}
}
}