PHP 处理 mht 文件

<?php
/**
 * Created by PhpStorm.
 * User: kungyu
 * Date: 2015/12/3
 * Time: 15:30
 */
class mhtparse {

    var $file = '';
    var $boundary = '';
    var $filedata = '';
    var $countparts = 1;
    var $log = '';

    function extract() {
        $this->read_filedata ();
        $this->file_parts ();

        return 1;
    }

    function set_file($p) {
        $this->file = $p;
    }

    function get_log() {
        return $this->log;
    }

    function file_parts() {
        $lines = explode ( "\n", substr ( $this->filedata, 0, 8192 ) );
        foreach ( $lines as $line ) {
            $line = trim ( $line );
            if (strpos ( $line, '=' ) !== FALSE) {
                if (strpos ( $line, 'boundary', 0 ) !== FALSE) {
                    $range = $this->getrange ( $line, '"', '"', 0 );
                    $this->boundary = "--" . $range ['range'];
                    $this->filedata = str_replace ( $line, '', $this->filedata );
                    break;
                }
            }
        }
        if ($this->boundary != '') {
            $this->filedata = explode ( $this->boundary, $this->filedata );
            unset ( $this->filedata [0] );
            $this->filedata = array_values ( $this->filedata );
            $this->countparts = count ( $this->filedata );
        } else {
            $tmp = $this->filedata;
            $this->filedata = array (
                $tmp
            );
        }
    }

    function get_all_part_file() {
        return $this->filedata;
    }

    function get_part_to_file($i) {
        $line_data_start = 0;
        $encoding = '';
        $part_lines = explode ( "\n", ltrim ( $this->filedata [$i] ) );
        foreach ( $part_lines as $line_id => $line ) {
            $line = trim ( $line );
            if ($line == '') {
                if (trim ( $part_lines [0] ) == '--')
                    return 1;
                $line_data_start = $line_id;
                break;
            }
            if (strpos ( $line, ':' ) !== FALSE) {
                $pos = strpos ( $line, ':' );
                $k = strtolower ( trim ( substr ( $line, 0, $pos ) ) );
                $v = trim ( substr ( $line, $pos + 1, strlen ( $line ) ) );
                if ($k == 'content-transfer-encoding') {
                    $encoding = $v;
                }
                if ($k == 'content-location') {
                    $location = $v;
                }
                if ($k == 'content-type') {
                    $contenttype = $v;
                }
            }
        }

        foreach ( $part_lines as $line_id => $line ) {
            if ($line_id <= $line_data_start)
                $part_lines [$line_id] = '';
        }

        $part_lines = implode ( '', $part_lines );
        if ($encoding == 'base64')
            $part_lines = base64_decode ( $part_lines );
        elseif ($encoding == 'quoted-printable')
            $part_lines = imap_qprint ( $part_lines );

        return $part_lines;
    }

    function read_filedata() {
        $handle = fopen ( $this->file, 'r' );
        $this->filedata = fread ( $handle, filesize ( $this->file ) );
        fclose ( $handle );
    }

    function getrange(&$subject, $Beginmark_str = '{', $Endmark_str = '}', $Start_pos = 0) {
        /*
         * $str="sssss { x { xx } {xx{xx } x} x} sssss"; $range=string::getRange($str,'{','}',0); echo $range['range']; //tulem: " x { xx } {xx{xx } x} x" echo $range['behin']; //tulem: 6 echo $range['end']; //tulem: 30 (' ') -- l5pumärgist järgnev out: array('range'=>$Range,'begin'=>$Begin_firstOccurence_pos,'end'=>$End_sequel_pos) | false v1.1 2004-2006,Uku-Kaarel J5esaar,ukjoesaar@hot.ee,http://www.hot.ee/ukjoesaar,+3725110693
         */
        if (empty ( $Beginmark_str ))
            $Beginmark_str = '{';
        $Beginmark_str_len = strlen ( $Beginmark_str );

        if (empty ( $Endmark_str ))
            $Endmark_str = '}';
        $Endmark_str_len = strlen ( $Endmark_str );

        /* $Start_pos_cache = 0; */
        do {
            /* !algus */
            if (! is_int ( $Begin_firstOccurence_pos ))
                $Start_pos_cache = $Start_pos;

            /* ?algus-test */
            $Start_pos_cache = @strpos ( $subject, $Beginmark_str, $Start_pos_cache );

            /* this is possible start for range */
            if (is_int ( $Start_pos_cache )) {
                /* skip */
                $Start_pos_cache = ($Start_pos_cache + $Beginmark_str_len);
                /* test possible range start pos */
                if (is_int ( $Begin_firstOccurence_pos )) {
                    if ($Start_pos_cache < $range_end_pos)
                        $rangeClean = 0;
                    elseif ($Start_pos_cache > $range_end_pos)
                        $rangeClean = 1;
                }
                /* here it is */
                if (! is_int ( $Begin_firstOccurence_pos ))
                    $Begin_firstOccurence_pos = $Start_pos_cache;
            } /* VIGA NR 0 ALGUST EI OLE */

            if (! is_int ( $Start_pos_cache )) {
                /* !algus */
                /* VIGA NR 1 ALGUSMARKI EI LEITUD : VIIMANE VOIMALIK ALGUS */
                if (is_int ( $Begin_firstOccurence_pos ) and ($Start_pos_cache < $range_end_pos))
                    $rangeClean = 1;
                else
                    return false;
            }
            if (is_int ( $Begin_firstOccurence_pos ) and ($rangeClean != 1)) {
                if (! is_int ( $End_pos_cache ))
                    $End_sequel_pos = $Begin_firstOccurence_pos;

                $End_pos_cache = strpos ( $subject, $Endmark_str, $End_sequel_pos );

                /* ok */
                if (is_int ( $End_pos_cache ) and ($rangeClean != 1)) {
                    $range_current_lenght = ($End_pos_cache - $Begin_firstOccurence_pos);
                    $End_sequel_pos = ($End_pos_cache + $Endmark_str_len);
                    $range_end_pos = $End_pos_cache;
                }
                /* VIGA NR 2 LOPPU EI LEITUD */
                if (! is_int ( $End_pos_cache ))
                    if ($End_pos_cache == false)
                        return false;
            }
        } while ( $rangeClean < 1 );

        if (is_int ( $Begin_firstOccurence_pos ) and is_int ( $range_current_lenght ))
            $Range = substr ( $subject, $Begin_firstOccurence_pos, $range_current_lenght );
        else
            return false;

        return array (
            'range' => $Range,
            'begin' => $Begin_firstOccurence_pos,
            'end' => $End_sequel_pos
        );
    } // end getrange()
} // class
/*$filename = './test.mht';
if (file_exists ( $filename )) {
    if (is_dir ( $filename )) return false;

    $filename = strtolower ( $filename );
    if (strpos ( $filename, '.mht', 1 ) == FALSE) return false;


    $o_mhtml = new mhtparse ();
    $o_mhtml->set_file ( $filename );
    $o_mhtml->extract ();
    $res =  $o_mhtml->get_part_to_file(0);
    var_dump($res);
}*/
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
安装.NET Framework失败后上网找的卸载工具,亲测卸载后可以完成安装.NET Framework。 一下位介绍: ====================== 分享可以完美卸载.NET Framework 1.0~4.5的framework卸载工具。.NET Framework几乎是系统比不可少的运行组件,特别是现在越来越多的软件都需要.NET Framework运行库的支持。例如本站给大家分享的coreldraw x6安装程序,就必须要.NET Framework 3.5、.NET Fra...... yinshu 分享可以完美卸载.NET Framework 1.0~4.5的framework卸载工具。.NET Framework几乎是系统比不可少的运行组件,特别是现在越来越多的软件都需要.NET Framework运行库的支持。例如本站给大家分享的coreldraw x6安装程序,就必须要.NET Framework 3.5、.NET Framework 4.0运行库的支持才可以安装及运行。 但最近有一台电脑,.NET Framework 4.0死活安装不上(可能是以前不完整安装造成的),想把Framework卸载了重新安装,结果系统自带的卸载工具还卸载不了,甚是头疼,后来找到这款专业的.net framework卸载工具:cleanup_tool才终于把net framework请出电脑,然后再次安装.NET Framework 4.0成功。 NET Framework卸载工具 .NET Framework Cleanup Tool软件简介 .NET Framework Cleanup Tool是一个专用于卸载、清除掉计算机中不同版本.NET框架的免费小软件。支持所有语言的.NET。它能删除与.NET相关的文件,并且还能自动将对应的文件夹、注册表项目和Windows Installer文件全部删除。 使用.NET Framework Cleanup Tool,基本可以将电脑还原到未安装.NET框架时的状态。因此,当你发现.NET不能正常更新,或是无法删除、甚至于重新安装不起作用时,试试这个小软件。 如何卸载framework cleanup_tool支持.NET Framework 1.0~4.5卸载,虽然是英文版的,不过使用简单,只要运行后选择你需要清除的.net framework版本(或者选择卸载全部.NET Framework版本),然后点击“cleanup now”按钮即可开始执行清除作业,过程会持续一到两分钟即可完成framework卸载。 特别注意:net framework框架并不是无用的东西,现在很多程序都需要net framework环境才能运行,所以建议你只有当.NET Framework损坏或者安装不上新版本时才考虑卸载重装。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值