PHP讀取文件,解決中文亂碼UTF-8的方法分析
本文實(shí)例講述了PHP讀取文件,解決中文亂碼UTF-8的方法。分享給大家供大家參考,具體如下:
$opts = array(
'file' => array(
'encoding' => "utf-8"
)
);
$opts = array('http' => array('encoding' => 'utf-8'));
$ctxt = stream_context_create($opts);
$content = file_get_contents($filePath, FILE_TEXT, $ctxt);
最簡(jiǎn)單的就是將GF2312→UTF-8
$str = iconv("gb2312", "utf-8", $str);
不管用的
$content = mb_convert_encoding($content, "UTF-8", "auto");
******************************************丑陋的分割線(xiàn)來(lái)告訴大家上面的不好的:下面的才是正確的方法···哈哈···**********************************************************
define('UTF32_BIG_ENDIAN_BOM', chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF));
define('UTF32_LITTLE_ENDIAN_BOM', chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00));
define('UTF16_BIG_ENDIAN_BOM', chr(0xFE) . chr(0xFF));
define('UTF16_LITTLE_ENDIAN_BOM', chr(0xFF) . chr(0xFE));
define('UTF8_BOM', chr(0xEF) . chr(0xBB) . chr(0xBF));
$text = file_get_contents($newPath);
$first2 = substr($text, 0, 2);
$first3 = substr($text, 0, 3);
$first4 = substr($text, 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';
$content = file_get_contents($newPath);
$content = iconv($encodType, "utf-8", $content);
終極版·····
$text = file_get_contents($filePath);
//$encodType = mb_detect_encoding($text);
define('UTF32_BIG_ENDIAN_BOM', chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF));
define('UTF32_LITTLE_ENDIAN_BOM', chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00));
define('UTF16_BIG_ENDIAN_BOM', chr(0xFE) . chr(0xFF));
define('UTF16_LITTLE_ENDIAN_BOM', chr(0xFF) . chr(0xFE));
define('UTF8_BOM', chr(0xEF) . chr(0xBB) . chr(0xBF));
$first2 = substr($text, 0, 2);
$first3 = substr($text, 0, 3);
$first4 = substr($text, 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';
//下面的判斷主要還是判斷ANSI編碼的·
if ($encodType == '') {//即默認(rèn)創(chuàng)建的txt文本-ANSI編碼的
$content = iconv("GBK", "UTF-8", $text);
} else if ($encodType == 'UTF-8 BOM') {//本來(lái)就是UTF-8不用轉(zhuǎn)換
$content = $text;
} else {//其他的格式都轉(zhuǎn)化為UTF-8就可以了
$content = iconv($encodType, "UTF-8", $text);
}
以上的終極版·可以適應(yīng)中文操作windows系統(tǒng)建立的ANSI``````````````UTF-8`````````Unicode`````的txt文本····
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php常用函數(shù)與技巧總結(jié)》及《PHP錯(cuò)誤與異常處理方法總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php偽協(xié)議實(shí)現(xiàn)命令執(zhí)行詳情
這篇文章主要介紹了php偽協(xié)議實(shí)現(xiàn)命令執(zhí)行詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,感興趣的朋友可以參考一下2022-06-06
PHP實(shí)現(xiàn)redis限制單ip、單用戶(hù)的訪(fǎng)問(wèn)次數(shù)功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)redis限制單ip、單用戶(hù)的訪(fǎng)問(wèn)次數(shù)功能,結(jié)合實(shí)例形式分析了php連接redis及獲取、記錄客戶(hù)端信息,并限制客戶(hù)訪(fǎng)問(wèn)次數(shù)等操作技巧,需要的朋友可以參考下2018-06-06

