【文档读取】TP5框架下,文档读取代码,可读取html,txt,mht,pdf,docx

这篇博客分享了在TP5框架下如何编写代码来读取不同类型的文档,包括HTML、TXT、MHT、PDF和DOCX文件,提供了实用的技术解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
     * 读取文档
     * @param $path
     * @return bool|string
     */
    static public function readDocument($path) {
        if(!$path) return false;
        
        if (IS_WIN) {
            ##windows下需要将路径转为GBK
            $filePath = iconv("UTF-8", "GBK", $path);
        } else {
            $filePath = $path;
        }
        
        if(file_exists($filePath)) {
            $content = file_get_contents($filePath);
            if(preg_match('/\.mht$/',$path)){
                $content = Utility::mht2html($content);
            }
        }
        else
            $content = false;
        return $content;
    }

    //转码为UTF-8
    static public function convert2UTF8($content) {
        $encodingList = array('UTF-8','GBK','GB2312');
        $encoding = mb_detect_encoding($content,$encodingList,true);

        if(!$encoding){
            $content = iconv('UCS-2', "UTF-8", $content);
            if(!$content) return false;       
        }elseif($encoding != 'UTF-8'){
            $content = mb_convert_encoding($content,'UTF-8',$encoding); 
        }
        $content = str_ireplace('gb2312','UTF-8',$content);
        return $content;
    }
	
	/*
     * 读取mht
     * @author 宋佳宇
     */
    static function mht2html($content) {
        $content_header = '/Content-Transfer-Encoding:([\s\S]*?)\n/ims';
        preg_match($content_header,$content,$item_results);
        if(!$item_results){
            return $content;
        }
        $bianma=trim($item_results[1]);
        if($bianma =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值