The following small series for everyone to bring a PHP parsing mht file converted to HTML instance. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
PHP parsing MHT files, using the editor to open can see Base64 encoding so, MHT can be converted to HTML.
<?php/** * For file parsing in MHT format * Use example: * * function Mhtmlparsebody ($filename) {if (file_exists ($filename)) {if (Is_dir ($f Ilename)) 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 = STRT Olower ($filename); if (Strpos ($filename, '. mht ', 1) = = false) return false; $o _mhtml = new MHTML (); $o _mhtml->set_f Ile ($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 () {$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->getran GE ($line, ' ', ' ', ', 0); $this->boundary = "--". $range [' Range ']; $this->filedata = Str_replace ($line, "', $this->filedata); if ($this->boundary! = ") {$this->filedata = explode ($this->boundary, $this->filedata); unset ($this->f Iledata [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->filedat A [$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 ($e ncoding = = ' 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-& Gt;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.php.cn/,+3725110693 */if (Empty ($Beginmark _str) $Beginmark _str = ' {'; $Beginmark _str_len = strlen ($Beginmark _str), if (Empty ($Endmark _str) $Endmark _str = '} '; $Endmar K_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_CAC he = @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_i NT ($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 V Oimalik Algus */if (is_int ($Begin _firstoccurence_pos) and ($Start _pos_cache < $range _end_pos)) $rangeClean = 1;elsere Turn 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_cach e) 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); Elsereturn false;return Array (' range ' = $Range, ' Begin ' = $Begin _firstoccurence_pos, ' end ' = $End _sequel_pos);} End GetRange ()}//Class?>