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


Posted in PHP onMarch 13, 2017

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的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
计数器详细设计
Oct 09 PHP
显示程序执行时间php函数代码
Aug 29 PHP
linux系统下php安装mbstring扩展的二种方法
Jan 20 PHP
Web程序工作原理详解
Dec 25 PHP
PHP答题类应用接口实例
Feb 09 PHP
详解PHP数组赋值方法
Nov 07 PHP
PHP实现伪静态方法汇总
Jan 13 PHP
PHP list() 将数组中的值赋给变量的简单实例
Jun 13 PHP
使用JavaScript创建新样式表和新样式规则
Jun 14 PHP
PHP实现上传图片到 zimg 服务器
Oct 19 PHP
基于yaf框架和uploadify插件,做的一个导入excel文件,查看并保存数据的功能
Jan 24 PHP
PHP中快速生成随机密码的几种方式
Apr 17 PHP
使用phpQuery获取数组的实例
Mar 13 #PHP
php获取linux命令结果的实例
Mar 13 #PHP
老生常谈PHP位运算的用途
Mar 12 #PHP
PHP巧妙利用位运算实现网站权限管理的方法
Mar 12 #PHP
解决PHP上传非标准格式的图片pjpeg失败的方法
Mar 12 #PHP
Yii2 加载css、js 载静态资源的方法
Mar 10 #PHP
php pdo操作数据库示例
Mar 10 #PHP
You might like
php 图像函数大举例(非原创)
2009/06/20 PHP
如何使用php输出时间格式
2013/08/31 PHP
PHPExcel在linux环境下导出报500错误的解决方法
2017/01/26 PHP
PHP针对中英文混合字符串长度判断及截取方法示例
2017/03/31 PHP
Thinkphp5 自定义上传文件名的实现方法
2019/07/23 PHP
Discuz! 6.1_jQuery兼容问题
2008/09/23 Javascript
Js获取事件对象代码
2010/08/05 Javascript
基于JQuery实现鼠标点击文本框显示隐藏提示文本
2012/02/23 Javascript
js模仿html5 placeholder适应于不支持的浏览器
2013/01/13 Javascript
js写出遮罩层登陆框和对联广告并自动跟随滚动条滚动
2014/04/29 Javascript
jQuery实现Email邮箱地址自动补全功能代码
2015/11/03 Javascript
JavaScript判断DIV内容是否为空的方法
2016/01/29 Javascript
在JavaScript中模拟类(class)及类的继承关系
2016/05/20 Javascript
Jquery根据浏览器窗口改变调整大小的方法
2017/02/07 Javascript
javascript 组合按键事件监听实现代码
2017/02/21 Javascript
详解前端路由实现与react-router使用姿势
2017/08/07 Javascript
vue实现微信分享链接添加动态参数的方法
2019/04/29 Javascript
antd Form组件方法getFieldsValue获取自定义组件的值操作
2020/10/29 Javascript
Python设计模式编程中解释器模式的简单程序示例分享
2016/03/02 Python
python脚本实现数据导出excel格式的简单方法(推荐)
2016/12/30 Python
Python探索之SocketServer详解
2017/10/28 Python
Python 记录日志的灵活性和可配置性介绍
2018/02/27 Python
Python基于递归算法求最小公倍数和最大公约数示例
2018/07/27 Python
python集合是否可变总结
2019/06/20 Python
keras的ImageDataGenerator和flow()的用法说明
2020/07/03 Python
45个非常奇妙的CSS3 特性应用示例
2012/01/01 HTML / CSS
举例详解CSS3中的Transition
2015/07/15 HTML / CSS
Kaufmann Mercantile官网:家居装饰、配件、户外及更多
2018/09/28 全球购物
泰国王权免税店官方网站:KingPower
2019/03/11 全球购物
初中新生军训方案
2014/05/13 职场文书
个人授权委托书样本
2014/09/13 职场文书
个人工作年终总结
2015/03/09 职场文书
史上最牛辞职信
2015/05/13 职场文书
休假证明书
2015/06/24 职场文书
2016年村干部公开承诺书(公开承诺事项)
2016/03/25 职场文书
win10截图快捷键win+shift+s没有反应无法截图怎么解决?
2022/08/14 数码科技