php解析mht文件转换成html的实例

PHP解析mht文件,使用编辑器打开可以看到base64编码所以,mht是可以转换成html的。

rush:PHP;"> /**

  • 针对Mht格式的文件进行解析

  • 使用例子:

  • function mhtmlParseBody($filename) {

    if (file_exists ( $filename )) {
    if (is_dir ( $filename )) return false;

      $filename = strtolower ( $filename );
      if (strpos ( $filename,'.mht',1 ) == FALSE) return false;
    
      $o_mhtml = new mhtml ();
      $o_mhtml->set_file ( $filename );
      $o_mhtml->extract ();
      return $o_mhtml->get_part_to_file(0);

    }
    return null;
    }

function mhtmlParseAll($filename) {

if (file_exists ( $filename )) {
    if (is_dir ( $filename )) return false;

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


    $o_mhtml = new mhtml ();
    $o_mhtml->set_file ( $filename );
    $o_mhtml->extract ();
    return $o_mhtml->get_all_part_file();

}
return null;

}
*/

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() {
    $li<a href="https://www.jb51.cc/tag/nes/" target="_blank" class="keywords">nes</a> = explode ( "\n",substr ( $this->filedata,8192 ) );
    foreach ( $li<a href="https://www.jb51.cc/tag/nes/" target="_blank" class="keywords">nes</a> 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_li<a href="https://www.jb51.cc/tag/nes/" target="_blank" class="keywords">nes</a> = explode ( "\n",ltrim ( $this->filedata [$i] ) );
    foreach ( $part_li<a href="https://www.jb51.cc/tag/nes/" target="_blank" class="keywords">nes</a> as $line_id => $line ) {
        $line = trim ( $line );
        if ($line == '') {
            if (trim ( $part_li<a href="https://www.jb51.cc/tag/nes/" target="_blank" class="keywords">nes</a> [0] ) == '--')
                return 1;
            $line_data_start = $line_id;
            break;
        }
        if (strpos ( $line,':' ) !== FALSE) {
            $pos = strpos ( $line,':' );
            $k = strtolower ( trim ( substr ( $line,$pos ) ) );
            $v = trim ( substr ( $line,$pos + 1,strlen ( $line ) ) );
            if ($k == 'content-<a href="https://www.jb51.cc/tag/transferencoding/" target="_blank" class="keywords">transfer-encoding</a>') {
                $encoding = $v;
            }
            if ($k == 'content-location') {
                $location = $v;
            }
            if ($k == 'content-type') {
                $contenttype = $v;
            }
        }
    }

    foreach ( $part_li<a href="https://www.jb51.cc/tag/nes/" target="_blank" class="keywords">nes</a> 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 = '}',<a href="https://www.jb51.cc/tag/start/" target="_blank" class="keywords">$start</a>_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&Atilde;¤rgist j&Atilde;¤rgnev out: array('range'=>$Range,'begin'=>$Begin_f<a href="https://www.jb51.cc/tag/irs/" target="_blank" class="keywords">irs</a>tOccurence_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 );

    /* <a href="https://www.jb51.cc/tag/start/" target="_blank" class="keywords">$start</a>_pos_cache = 0; */
    do {
        /* !algus */
        if (! is_int ( $Begin_f<a href="https://www.jb51.cc/tag/irs/" target="_blank" class="keywords">irs</a>tOccurence_pos ))
            <a href="https://www.jb51.cc/tag/start/" target="_blank" class="keywords">$start</a>_pos_cache = <a href="https://www.jb51.cc/tag/start/" target="_blank" class="keywords">$start</a>_pos;

            /* ?algus-test */
        <a href="https://www.jb51.cc/tag/start/" target="_blank" class="keywords">$start</a>_pos_cache = @strpos ( $subject,$Beginmark_str,<a href="https://www.jb51.cc/tag/start/" target="_blank" class="keywords">$start</a>_pos_cache );

        /* this is possible start for range */
        if (is_int ( <a href="https://www.jb51.cc/tag/start/" target="_blank" class="keywords">$start</a>_pos_cache )) {
            /* skip */
            <a href="https://www.jb51.cc/tag/start/" target="_blank" class="keywords">$start</a>_pos_cache = (<a href="https://www.jb51.cc/tag/start/" target="_blank" class="keywords">$start</a>_pos_cache + $Beginmark_str_len);
            /* test possible range start pos */
            if (is_int ( $Begin_f<a href="https://www.jb51.cc/tag/irs/" target="_blank" class="keywords">irs</a>tOccurence_pos )) {
                if (<a href="https://www.jb51.cc/tag/start/" target="_blank" class="keywords">$start</a>_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 ( <a href="https://www.jb51.cc/tag/start/" target="_blank" class="keywords">$start</a>_pos_cache )) {
            /* !algus */
/* VIGA NR 1 ALGUSMARKI EI LEITUD : VIIMANE VOIMALIK ALGUS */
if (is_int ( $Begin_f<a href="https://www.jb51.cc/tag/irs/" target="_blank" class="keywords">irs</a>tOccurence_pos ) and (<a href="https://www.jb51.cc/tag/start/" target="_blank" class="keywords">$start</a>_pos_cache < $range_end_pos))
                $rangeClean = 1;
            else
                return false;
        }
        if (is_int ( $Begin_f<a href="https://www.jb51.cc/tag/irs/" target="_blank" class="keywords">irs</a>tOccurence_pos ) and ($rangeClean != 1)) {
            if (! is_int ( $End_pos_cache ))
                $End_sequel_pos = $Begin_f<a href="https://www.jb51.cc/tag/irs/" target="_blank" class="keywords">irs</a>tOccurence_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_f<a href="https://www.jb51.cc/tag/irs/" target="_blank" class="keywords">irs</a>tOccurence_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_f<a href="https://www.jb51.cc/tag/irs/" target="_blank" class="keywords">irs</a>tOccurence_pos,'end' => $End_sequel_pos 
    );
} // end getrange()

} // class
?>

以上这篇PHP解析mht文件转换成html的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


服务器优化必备:深入了解PHP8底层开发原理
Golang的网络编程:如何快速构建高性能的网络应用?
Golang和其他编程语言的对比:为什么它的开发效率更高?
PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用
将字符重新排列以形成回文(如果可能)在C++中
掌握PHP8底层开发原理和新特性:创建高效可扩展的应用程序
服务器性能优化必学:掌握PHP8底层开发原理
PHP8新特性和底层开发原理详解:优化应用性能的终极指南
将 C/C++ 代码转换为汇编语言
深入研究PHP8底层开发原理:创建高效可扩展的应用程序
C++程序查找法向量和迹
PHP8底层开发原理实战指南:提升服务器效能
重排数组,使得当 i 为偶数时,arr[i] >= arr[j],当 i 为奇数时,arr[i] <= arr[j],其中 j < i,使用 C++ 语言实现
Golang的垃圾回收:为什么它可以减少开发人员的负担?
C++程序:将一个数组的所有元素复制到另一个数组中
Golang:构建智能系统的基石
为什么AI开发者应该关注Golang?
在C和C++中,逗号(comma)的用法是用来分隔表达式或语句
PHP8底层开发原理解析及新特性应用实例
利用PHP8底层开发原理解析新特性:如何构建出色的Web应用