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 相关文章推荐
56.com视频采集接口程序(PHP)
Sep 22 PHP
php imagecreatetruecolor 创建高清和透明图片代码小结
May 15 PHP
php 阴历-农历-转换类代码
Jan 16 PHP
深入分析php中接口与抽象类的区别
Jun 08 PHP
解析php中获取url与物理路径的总结
Jun 21 PHP
解析php中获取系统信息的方法
Jun 25 PHP
PHP中批量生成静态html(命令行下运行PHP)
Apr 19 PHP
使用PHP Socket 编程模拟Http post和get请求
Nov 25 PHP
Session 失效的原因汇总及解决丢失办法
Sep 30 PHP
Laravel5框架自定义错误页面配置操作示例
Apr 17 PHP
PHP从零开始打造自己的MVC框架之路由类实现方法分析
Jun 03 PHP
PHP实现Snowflake生成分布式唯一ID的方法示例
Aug 30 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搜索文件程序分享
2015/10/30 PHP
Javascript生成json的函数代码(可以用php的json_decode解码)
2012/06/11 Javascript
有关于JS辅助函数inherit()的问题
2013/04/07 Javascript
JS批量修改PS中图层名称的方法
2014/01/26 Javascript
JavaScript取得键盘按下方向键是哪个的方法
2015/08/04 Javascript
javascript实现简单计算器效果【推荐】
2016/04/19 Javascript
plupload+artdialog实现多平台上传文件
2016/07/19 Javascript
BootStrap 弹出层代码
2017/02/09 Javascript
微信小程序中实现一对多发消息详解及实例代码
2017/02/14 Javascript
react-router JS 控制路由跳转实例
2017/06/15 Javascript
JS实现的简单分页功能示例
2018/08/23 Javascript
js实现图片放大并跟随鼠标移动特效
2019/01/18 Javascript
layui table 列宽百分比显示的实现方法
2019/09/28 Javascript
构建Vue大型应用的10个最佳实践(小结)
2019/11/07 Javascript
js最全的数组的降维5种办法(小结)
2020/04/28 Javascript
[01:25]DOTA2超级联赛专访iG 将调整状态找回自己
2013/06/05 DOTA
python共享引用(多个变量引用)示例代码
2013/12/04 Python
跟老齐学Python之模块的加载
2014/10/24 Python
为Python的Tornado框架配置使用Jinja2模板引擎的方法
2016/06/30 Python
Python实现小数转化为百分数的格式化输出方法示例
2017/09/20 Python
Python玩转Excel的读写改实例
2019/02/22 Python
从列表或字典创建Pandas的DataFrame对象的方法
2019/07/06 Python
Django后台admin的使用详解
2019/07/08 Python
face++与python实现人脸识别签到(考勤)功能
2019/08/28 Python
利用python中集合的唯一性实现去重
2020/02/11 Python
html5+css3之CSS中的布局与Header的实现
2014/11/21 HTML / CSS
浅谈HTML5 &amp; CSS3的新交互特性
2016/07/19 HTML / CSS
Becextech新西兰:数码单反相机和手机在线商店
2018/04/27 全球购物
俄罗斯外国汽车和国产汽车配件网上商店:Движком
2020/04/19 全球购物
幼儿运动会邀请函
2014/01/17 职场文书
青年文明号事迹材料
2014/01/18 职场文书
幼儿园教师节活动方案
2014/02/02 职场文书
对孩子的寄语
2014/04/09 职场文书
《狼和小羊》教学反思
2014/04/20 职场文书
关于安全演讲稿
2014/05/09 职场文书
写自招自荐信的绝招!
2019/04/19 职场文书