posts - 495,  comments - 11,  trackbacks - 0

          注意:要把php.ini中 extension=php_mbstring.dll 前的;號去掉,重啟apache就可以了。
          我創(chuàng)建三個文件:text1.txt?? text2.txt?? text3.txt
          分別以ASCII? UTF-8? UNICODE 的編碼方式保存

          ?

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

          function detect_utf_encoding($text) {
          ??? $first2 = substr($text, 0, 2);
          ??? $first3 = substr($text, 0, 3);
          ??? $first4 = substr($text, 0, 3);
          ? ?
          ??? if ($first3 == UTF8_BOM) return 'UTF-8';
          ??? elseif ($first4 == UTF32_BIG_ENDIAN_BOM) return 'UTF-32BE';
          ??? elseif ($first4 == UTF32_LITTLE_ENDIAN_BOM) return 'UTF-32LE';
          ??? elseif ($first2 == UTF16_BIG_ENDIAN_BOM) return 'UTF-16BE';
          ??? elseif ($first2 == UTF16_LITTLE_ENDIAN_BOM) return 'UTF-16LE';
          }

          function getFileEncoding($str){
          ??? $encoding=mb_detect_encoding($str);
          ??? if(empty($encoding)){
          ??????? $encoding=detect_utf_encoding($str);
          ??? }
          ??? return $encoding;
          }

          $file = 'text1.txt';
          echo getFileEncoding(file_get_contents($file));? // 輸出ASCII
          echo '<br />';

          $file = 'text2.txt';
          echo getFileEncoding(file_get_contents($file));? // 輸出UTF-8
          echo '<br />';


          $file = 'text3.txt';
          echo getFileEncoding(file_get_contents($file));? // 輸出UTF-16LE
          echo '<br />';
          ?>


          posted on 2011-08-12 20:16 jadmin 閱讀(169) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 托克托县| 芷江| 綦江县| 阳高县| 吉林省| 平阳县| 富顺县| 营口市| 枣强县| 镇平县| 陕西省| 潮州市| 马关县| 收藏| 乡宁县| 奉化市| 孝感市| 会同县| 侯马市| 红河县| 普安县| 黄浦区| 南岸区| 茌平县| 石门县| 西充县| 上杭县| 太谷县| 林口县| 合阳县| 雷州市| 禄劝| 河源市| 宜兰县| 胶南市| 安岳县| 大港区| 菏泽市| 南江县| 集贤县| 高州市|