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 相关文章推荐
php MySQL与分页效率
Jun 04 PHP
php at(@)符号的用法简介
Jul 11 PHP
PHP数组内存耗用太多问题的解决方法
Apr 05 PHP
PHP XML操作的各种方法解析(比较详细)
Jun 17 PHP
PHP数组交集的优化代码分析
Mar 06 PHP
php一些错误处理的方法与技巧总结
Aug 10 PHP
Codeigniter操作数据库表的优化写法总结
Jun 12 PHP
使用PHPExcel操作Excel用法实例分析
Mar 26 PHP
3种php生成唯一id的方法
Nov 23 PHP
PHP正则替换函数preg_replace()报错:Notice Use of undefined constant的解决方法分析
Feb 04 PHP
php用xpath解析html的代码实例讲解
Feb 14 PHP
php+Ajax无刷新验证用户名操作实例详解
Mar 04 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
javascript转换字符串为dom对象(字符串动态创建dom)
2010/05/10 Javascript
解决jquery1.9不支持browser对象的问题
2013/11/13 Javascript
JavaScript SetInterval与setTimeout使用方法详解
2013/11/15 Javascript
JQuery处理json与ajax返回JSON实例代码
2014/01/03 Javascript
js调试系列 源码定位与调试[基础篇]
2014/06/18 Javascript
javascript实现左右控制无缝滚动
2014/12/31 Javascript
jQuery图片轮播功能实例代码
2017/01/29 Javascript
php输出全部gb2312编码内的汉字方法
2017/03/04 Javascript
Vue响应式原理详解
2017/04/18 Javascript
mock.js实现模拟生成假数据功能示例
2019/01/15 Javascript
layui在form表单页面通过Validform加入简单验证的方法
2019/09/06 Javascript
如何在微信小程序中使用骨架屏的步骤
2020/06/12 Javascript
[42:39]老党炸弹人试玩视频
2014/09/03 DOTA
Python按行读取文件的实现方法【小文件和大文件读取】
2016/09/19 Python
Python使用遗传算法解决最大流问题
2018/01/29 Python
python实现多线程行情抓取工具的方法
2018/02/28 Python
Python中logging实例讲解
2019/01/17 Python
Python openpyxl读取单元格字体颜色过程解析
2019/09/03 Python
Python中低维数组填充高维数组的实现
2019/12/02 Python
PyCharm使用之配置SSH Interpreter的方法步骤
2019/12/26 Python
python将logging模块封装成单独模块并实现动态切换Level方式
2020/05/12 Python
解决echarts中饼图标签重叠的问题
2020/05/16 Python
给Django Admin添加验证码和多次登录尝试限制的实现
2020/07/26 Python
实例教程 纯CSS3打造非常炫的加载动画效果
2014/11/05 HTML / CSS
css3的transform造成z-index无效解决方案
2014/12/04 HTML / CSS
用HTML5 Canvas API中的clearRect()方法实现橡皮擦功能
2016/03/15 HTML / CSS
Toppik顶丰增发纤维官网:解决头发稀疏
2017/12/30 全球购物
SportsDirect.com马来西亚:英国第一体育零售商
2018/11/21 全球购物
波兰在线体育用品商店:Hop-Sport.pl
2019/07/23 全球购物
管理学专业个人求职信范文
2013/09/21 职场文书
国庆节文艺活动方案
2014/02/03 职场文书
关于孝道的演讲稿
2014/05/21 职场文书
法人身份证明书
2014/10/08 职场文书
公文格式,规则明细(新手收藏)
2019/07/23 职场文书
TensorFlow的自动求导原理分析
2021/05/26 Python
MySQL聚簇索引和非聚簇索引的区别详情
2022/06/14 MySQL