PHP读取文件,解决中文乱码UTF-8

[php] view plain copy print ?
  1. $opts = array(  
  2.     'file' => array(  
  3.         'encoding' => "utf-8"  
  4.     )  
  5. );  
  6. $opts = array('http' => array('encoding' => 'utf-8'));  
  7. $ctxt = stream_context_create($opts);  
  8. $content = file_get_contents($filePath, FILE_TEXT, $ctxt);  

最简单的就是将GF2312→UTF-8


[php] view plain copy print ?
  1. $str = iconv("gb2312""utf-8"$str);  

不管用的


[php] view plain copy print ?
  1. $content = mb_convert_encoding($content"UTF-8""auto");  


******************************************丑陋的分割线来告诉大家上面的不好的:下面的才是正确的方法···哈哈···**********************************************************



[php] view plain copy print ?
  1. define('UTF32_BIG_ENDIAN_BOM'chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF));  
  2. define('UTF32_LITTLE_ENDIAN_BOM'chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00));  
  3. define('UTF16_BIG_ENDIAN_BOM'chr(0xFE) . chr(0xFF));  
  4. define('UTF16_LITTLE_ENDIAN_BOM'chr(0xFF) . chr(0xFE));  
  5. define('UTF8_BOM'chr(0xEF) . chr(0xBB) . chr(0xBF));  
  6.   
  7. $text = file_get_contents($newPath);  
  8. $first2 = substr($text, 0, 2);  
  9. $first3 = substr($text, 0, 3);  
  10. $first4 = substr($text, 0, 3);  
  11. $encodType = "";  
  12. if ($first3 == UTF8_BOM)  
  13.     $encodType = 'UTF-8 BOM';  
  14. else if ($first4 == UTF32_BIG_ENDIAN_BOM)  
  15.     $encodType = 'UTF-32BE';  
  16. else if ($first4 == UTF32_LITTLE_ENDIAN_BOM)  
  17.     $encodType = 'UTF-32LE';  
  18. else if ($first2 == UTF16_BIG_ENDIAN_BOM)  
  19.     $encodType = 'UTF-16BE';  
  20. else if ($first2 == UTF16_LITTLE_ENDIAN_BOM)  
  21.     $encodType = 'UTF-16LE';  
  22.   
  23. $content = file_get_contents($newPath);  
  24.   
  25. $content = iconv($encodType"utf-8"$content);  


终极版·····

[php] view plain copy print ?
  1. $text = file_get_contents($filePath);  
  2.                         //$encodType = mb_detect_encoding($text);  
  3.                         define('UTF32_BIG_ENDIAN_BOM'chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF));  
  4.                         define('UTF32_LITTLE_ENDIAN_BOM'chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00));  
  5.                         define('UTF16_BIG_ENDIAN_BOM'chr(0xFE) . chr(0xFF));  
  6.                         define('UTF16_LITTLE_ENDIAN_BOM'chr(0xFF) . chr(0xFE));  
  7.                         define('UTF8_BOM'chr(0xEF) . chr(0xBB) . chr(0xBF));  
  8.                         $first2 = substr($text, 0, 2);  
  9.                         $first3 = substr($text, 0, 3);  
  10.                         $first4 = substr($text, 0, 3);  
  11.                         $encodType = "";  
  12.                         if ($first3 == UTF8_BOM)  
  13.                             $encodType = 'UTF-8 BOM';  
  14.                         else if ($first4 == UTF32_BIG_ENDIAN_BOM)  
  15.                             $encodType = 'UTF-32BE';  
  16.                         else if ($first4 == UTF32_LITTLE_ENDIAN_BOM)  
  17.                             $encodType = 'UTF-32LE';  
  18.                         else if ($first2 == UTF16_BIG_ENDIAN_BOM)  
  19.                             $encodType = 'UTF-16BE';  
  20.                         else if ($first2 == UTF16_LITTLE_ENDIAN_BOM)  
  21.                             $encodType = 'UTF-16LE';  
  22.   
  23.                         //下面的判断主要还是判断ANSI编码的·  
  24.                         if ($encodType == '') {//即默认创建的txt文本-ANSI编码的  
  25.                             $content = iconv("GBK""UTF-8"$text);  
  26.                         } else if ($encodType == 'UTF-8 BOM') {//本来就是UTF-8不用转换  
  27.                             $content = $text;  
  28.                         } else {//其他的格式都转化为UTF-8就可以了  
  29.                             $content = iconv($encodType"UTF-8"$text);  
  30.                         }  

以上的终极版·可以适应中文操作windows系统建立的ANSI``````````````UTF-8`````````Unicode`````的txt文本····

你可能感兴趣的:(IT详情解答,php(2))