美文网首页
PHP解决中文乱码

PHP解决中文乱码

作者: 冷掌柜 | 来源:发表于2018-12-07 14:39 被阅读0次
  • PHP解决中文乱码
            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($content, 0, 2);
            $first3 = substr($content, 0, 3);
            $first4 = substr($content, 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", $content);
            } else if ($encodType == 'UTF-8 BOM') {//本来就是UTF-8不用转换
                $content = $content;
            } else {//其他的格式都转化为UTF-8就可以了
                $content = iconv($encodType, "UTF-8", $content);
            }

相关文章

网友评论

      本文标题:PHP解决中文乱码

      本文链接:https://www.haomeiwen.com/subject/bidjzftx.html