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

发布时间 - 2026-01-11 00:08:11    点击率:

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


# mht文件转换成html  # PHP转换文本框内容为HTML格式的方法  # php实现转换html格式为文本格式的方法  # php生成html文件方法总结  # 给大家  # 希望能  # 可以看到  # 这篇  # 转换成  # 编辑器  # 小编  # 大家多多  # 文件转换成  # foreach  # explode  # substr  # getrange  # range  # line  # trim  # countparts  # log  # boundary  # filedata 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Windows Hello人脸识别突然无法使用  如何在新浪SAE免费搭建个人博客?  如何在Windows虚拟主机上快速搭建网站?  详解Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点  Python文本处理实践_日志清洗解析【指导】  Laravel的HTTP客户端怎么用_Laravel HTTP Client发起API请求教程  文字头像制作网站推荐软件,醒图能自动配文字吗?  php打包exe后无法访问网络共享_共享权限设置方法【教程】  laravel怎么在请求结束后执行任务(Terminable Middleware)_laravel Terminable Middleware请求结束任务执行方法  什么是javascript作用域_全局和局部作用域有什么区别?  javascript中的try catch异常捕获机制用法分析  JavaScript常见的五种数组去重的方式  JavaScript实现Fly Bird小游戏  Laravel如何实现邮件验证激活账户_Laravel内置MustVerifyEmail接口配置【步骤】  高性能网站服务器部署指南:稳定运行与安全配置优化方案  linux top下的 minerd 木马清除方法  Laravel怎么实现模型属性转换Casting_Laravel自动将JSON字段转为数组【技巧】  Laravel如何为API生成Swagger或OpenAPI文档  JavaScript中如何操作剪贴板_ClipboardAPI怎么用  如何在阿里云高效完成企业建站全流程?  绝密ChatGPT指令:手把手教你生成HR无法拒绝的求职信  企业在线网站设计制作流程,想建设一个属于自己的企业网站,该如何去做?  Windows10如何删除恢复分区_Win10 Diskpart命令强制删除分区  html5audio标签播放结束怎么触发事件_onended回调方法【教程】  Laravel如何使用集合(Collections)进行数据处理_Laravel Collection常用方法与技巧  Laravel怎么集成Vue.js_Laravel Mix配置Vue开发环境  哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?  Laravel如何使用Eloquent进行子查询  Angular 表单中正确绑定输入值以确保提交与验证正常工作  Laravel如何使用Laravel Vite编译前端_Laravel10以上版本前端静态资源管理【教程】  Laravel如何使用Socialite实现第三方登录?(微信/GitHub示例)  猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?  如何在云主机快速搭建网站站点?  谷歌Google入口永久地址_Google搜索引擎官网首页永久入口  html5如何实现懒加载图片_ intersectionobserver api用法【教程】  如何在 Telegram Web View(iOS)中防止键盘遮挡底部输入框  利用vue写todolist单页应用  香港服务器网站卡顿?如何解决网络延迟与负载问题?  🚀拖拽式CMS建站能否实现高效与个性化并存?  深圳网站制作公司好吗,在深圳找工作哪个网站最好啊?  南京网站制作费用,南京远驱官方网站?  laravel怎么为应用开启和关闭维护模式_laravel应用维护模式开启与关闭方法  Edge浏览器怎么启用睡眠标签页_节省电脑内存占用优化技巧  Java解压缩zip - 解压缩多个文件或文件夹实例  Laravel如何自定义分页视图?(Pagination示例)  Win11怎么关闭专注助手 Win11关闭免打扰模式设置【操作】  如何用AI帮你把自己的生活经历写成一个有趣的故事?  高端企业智能建站程序:SEO优化与响应式模板定制开发  使用PHP下载CSS文件中的所有图片【几行代码即可实现】  Laravel全局作用域是什么_Laravel Eloquent Global Scopes应用指南