php识别文件编码,并读出内容,对大文件也很有效率



", $str);
        } else {
            die('文件路径错误!');
        }
    }
    
    /**
     * 获取文件编码类型
     * @param  string $file_path    文件路径
     * @param  string $filesize     需要获取的字符长度
     * @return string               返回字符编码
     */
    function detect_encoding($file_path, $filesize = '1000') {
        $list = array('GBK', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'ISO-8859-1');
        $str = fileToSrting($file_path, $filesize);
        foreach ($list as $item) {
            $tmp = mb_convert_encoding($str, $item, $item);
            if (md5($tmp) == md5($str)) {
                return $item;
            }
        }
        return '遇到识别不出来的编码!';
    }
    /**
     * 自动解析编码读入文件
     * @param string $file_path 文件路径
     * @param string $charset 读取编码
     * @return string 返回读取内容
     */
    function auto_read($file_path, $filesize = '', $charset = 'UTF-8') {
        $list = array('GBK', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'ISO-8859-1');
        $str = fileToSrting($file_path, $filesize);
        foreach ($list as $item) {
            $tmp = mb_convert_encoding($str, $item, $item);
            if (md5($tmp) == md5($str)) {
                return mb_convert_encoding($str, $charset, $item);
            }
        }
        return "";
    }
    
    $file_path = "鸟哥的linux私房菜第二版包括基础和服务器.chm";
    echo "
";
    $time1 = microtime(true);
    print_r(detect_encoding($file_path, 10));
    $time2 = microtime(true);
    printf("识别编码时间为:%s 秒 \n", $time2 - $time1);
    exit;
    
    echo "\n";
    echo "\n";
    echo "\n";
    echo "\n";
    print_r(auto_read($file));
    echo "
"; $time3 = microtime(true); printf("将文件读出到字符串时间为:%s 秒 \n", $time3 - $time2); exit;

原文:https://blog.csdn.net/zhezhebie/article/details/72732453

你可能感兴趣的:(php,知识点)