欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-13 16:08:57
php解析mht文件,使用编辑器打开可以看到base64编码所以,mht是可以转换成html的。

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

<?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, '.mht', 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() {
		$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
?>

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