您好,登錄后才能下訂單哦!
小編給大家分享一下PHP解決中文亂碼UTF-8的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
php的框架:1、Laravel,Laravel是一款免費并且開源的PHP應(yīng)用框架。2、Phalcon,Phalcon是運行速度最快的一個PHP框架。3、Symfony,Symfony是一款為Web項目準(zhǔn)備的PHP框架。4、Yii,Yii是一款快速、安全和專業(yè)的PHP框架。5、CodeIgniter,CodeIgniter是一款非常敏捷的開源PHP框架。6、CakePHP,CakePHP是一款老牌的PHP框架。7.Kohana,Kohana是一款敏捷但是功能強大的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 == '') {//即默認(rèn)創(chuàng)建的txt文本-ANSI編碼的 $content = iconv("GBK", "UTF-8", $text); } else if ($encodType == 'UTF-8 BOM') {//本來就是UTF-8不用轉(zhuǎn)換 $content = $text; } else {//其他的格式都轉(zhuǎn)化為UTF-8就可以了 $content = iconv($encodType, "UTF-8", $text); }
以上是“PHP解決中文亂碼UTF-8的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。