1. 程式人生 > 程式設計 >PHP讀取檔案,解決中文亂碼UTF-8的方法分析

PHP讀取檔案,解決中文亂碼UTF-8的方法分析

本文例項講述了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,2);
$first3 = substr($text,3);
$first4 = substr($text,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,$content);

終極版·····

$text = file_get_contents($filePath);
//$encodType = mb_detect_encoding($text);
define('UTF32_BIG_ENDIAN_BOM',chr(0xEF) . chr(0xBB) . chr(0xBF));
$first2 = substr($text,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",$text);
} else if ($encodType == 'UTF-8 BOM') {//本來就是UTF-8不用轉換
  $content = $text;
} else {//其他的格式都轉化為UTF-8就可以了
  $content = iconv($encodType,$text);
}

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

更多關於PHP相關內容感興趣的讀者可檢視本站專題:《PHP編碼與轉碼操作技巧彙總》、《PHP陣列(Array)操作技巧大全》、《php字串(string)用法總結》、《php常用函式與技巧總結》及《PHP錯誤與異常處理方法總結》

希望本文所述對大家PHP程式設計有所幫助。