首頁  >  文章  >  後端開發  >  怎麼解決php讀取檔亂碼問題

怎麼解決php讀取檔亂碼問題

藏色散人
藏色散人原創
2020-11-20 09:51:102682瀏覽

php讀取檔案亂碼的解決方法:先開啟對應的程式碼檔案;然後透過「iconv($encodType, "utf-8", $content);  」方法解決中文亂碼即可。

怎麼解決php讀取檔亂碼問題

推薦:《PHP影片教學

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);

最簡單的就是將GF2312→UTF-8

$str=iconv("gb2312", "utf-8", $str);

不管用的

$content
 = mb_convert_encoding(
$content
, 
"UTF-8"
, 
"auto"
);

*************** ***************************醜陋的分割線來告訴大家上面的不好的:下面的才是正確的方法···············哈哈···********************************************** ************

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 == '') {//即默认创建的txt文本-ANSI编码的  
                            $content = iconv("GBK", "UTF-8", $text);  
                        } else if ($encodType == 'UTF-8 BOM') {//本来就是UTF-8不用转换  
                            $content = $text;  
                        } else {//其他的格式都转化为UTF-8就可以了  
                            $content = iconv($encodType, "UTF-8", $text);  
                        }

以上的終極版·可以適應中文操作windows系統建立的ANSI"""" "``UTF-8"""Unicode"``的txt文字····

#

以上是怎麼解決php讀取檔亂碼問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn