登录  /  注册

php解析mht文件转换成html的方法

墨辰丷
发布: 2018-05-24 17:31:02
原创
2840人浏览过

本篇文章主要介绍php解析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, &#39;.mht&#39;, 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, &#39;.mht&#39;, 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 = &#39;&#39;;
	var $boundary = &#39;&#39;;
	var $filedata = &#39;&#39;;
	var $countparts = 1;
	var $log = &#39;&#39;;
	
	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, &#39;=&#39; ) !== FALSE) {
				if (strpos ( $line, &#39;boundary&#39;, 0 ) !== FALSE) {
					$range = $this->getrange ( $line, &#39;"&#39;, &#39;"&#39;, 0 );
					$this->boundary = "--" . $range [&#39;range&#39;];
					$this->filedata = str_replace ( $line, &#39;&#39;, $this->filedata );
					break;
				}
			}
		}
		if ($this->boundary != &#39;&#39;) {
			$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 = &#39;&#39;;
		$part_lines = explode ( "\n", ltrim ( $this->filedata [$i] ) );
		foreach ( $part_lines as $line_id => $line ) {
			$line = trim ( $line );
			if ($line == &#39;&#39;) {
				if (trim ( $part_lines [0] ) == &#39;--&#39;)
					return 1;
				$line_data_start = $line_id;
				break;
			}
			if (strpos ( $line, &#39;:&#39; ) !== FALSE) {
				$pos = strpos ( $line, &#39;:&#39; );
				$k = strtolower ( trim ( substr ( $line, 0, $pos ) ) );
				$v = trim ( substr ( $line, $pos + 1, strlen ( $line ) ) );
				if ($k == &#39;content-transfer-encoding&#39;) {
					$encoding = $v;
				}
				if ($k == &#39;content-location&#39;) {
					$location = $v;
				}
				if ($k == &#39;content-type&#39;) {
					$contenttype = $v;
				}
			}
		}
		
		foreach ( $part_lines as $line_id => $line ) {
			if ($line_id <= $line_data_start)
				$part_lines [$line_id] = &#39;&#39;;
		}
		
		$part_lines = implode ( &#39;&#39;, $part_lines );
		if ($encoding == &#39;base64&#39;)
			$part_lines = base64_decode ( $part_lines );
		elseif ($encoding == &#39;quoted-printable&#39;)
			$part_lines = imap_qprint ( $part_lines );
		
		return $part_lines;
	}
	
	function read_filedata() {
		$handle = fopen ( $this->file, &#39;r&#39; );
		$this->filedata = fread ( $handle, filesize ( $this->file ) );
		fclose ( $handle );
	}
	
	function getrange(&$subject, $Beginmark_str = &#39;{&#39;, $Endmark_str = &#39;}&#39;, $Start_pos = 0) {
		/*
		 * $str="sssss { x { xx } {xx{xx } x} x} sssss"; $range=string::getRange($str,&#39;{&#39;,&#39;}&#39;,0); echo $range[&#39;range&#39;]; //tulem: " x { xx } {xx{xx } x} x" echo $range[&#39;behin&#39;]; //tulem: 6 echo $range[&#39;end&#39;]; //tulem: 30 (&#39; &#39;) -- l5pumärgist järgnev out: array(&#39;range&#39;=>$Range,&#39;begin&#39;=>$Begin_firstOccurence_pos,&#39;end&#39;=>$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 = &#39;{&#39;;
		$Beginmark_str_len = strlen ( $Beginmark_str );
		
		if (empty ( $Endmark_str ))
			$Endmark_str = &#39;}&#39;;
		$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 (
				&#39;range&#39; => $Range,
				&#39;begin&#39; => $Begin_firstOccurence_pos,
				&#39;end&#39; => $End_sequel_pos 
		);
	} // end getrange()
} // class
?>
登录后复制

以上就是本文的全部内容,希望对大家的学习有所帮助。

相关推荐:

PHP转换Base64图片为本地图片并保存

PHP转换超过2038年日期出错如何解决

php转换ubb代码的实例

以上就是php解析mht文件转换成html的方法的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号