zoukankan      html  css  js  c++  java
  • php -- php检测文件编码的方法示例

    <?php
     /**
    * 检测文件编码
     * @param string $file 文件路径
    * @return string|null 返回 编码名 或 null
    */
     function detect_encoding($file) {
         $list = array('GBK', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'ISO-8859-1');
         $str = file_get_contents($file);
         foreach ($list as $item) {
             $tmp = mb_convert_encoding($str, $item, $item);
             if (md5($tmp) == md5($str)) {
                 return $item;
             }
         }
         return null;
    }
    
    /**
    * 自动解析编码读入文件
    * @param string $file 文件路径
    * @param string $charset 读取编码
    * @return string 返回读取内容
    */
    function auto_read($file, $charset='UTF-8') {
        $list = array('GBK', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'ISO-8859-1');
         $str = file_get_contents($file);
         foreach ($list as $item) {
            $tmp = mb_convert_encoding($str, $item, $item);
            if (md5($tmp) == md5($str)) {
                 return mb_convert_encoding($str, $charset, $item);
             }
         }
        return "";
    }
  • 相关阅读:
    C open fopen read fread
    图像混合模式算法
    高级API和低级API
    strcpy_s与strcpy
    IsPowerOfTwo
    透明度算法
    POJ 2240(bellman_ford)
    POJ 1797(dijkstra)
    【转载】POJ 图论题目列表
    POJ 1502(Floyd)
  • 原文地址:https://www.cnblogs.com/hf8051/p/4740183.html
Copyright © 2011-2022 走看看