python解析mht文件_php解析mht文件转换成html的方法
本篇文章主要介绍php解析mht文件转换成html的方法,感兴趣的朋友参考下,希望对大家有所帮助。
php解析mht文件,使用编辑器打开可以看到base64编码所以,mht是可以转换成html的。
/**
* 针对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
?>
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
python解析mht文件_php解析mht文件转换成html的方法相关推荐
- python转json的函数_python将字符串转换成json的方法小结
最近在工作中遇到了一个小问题,如果要将字符串型的数据转换成dict类型,我第一时间就想到了使用json函数.但是里面出现了一些问题 1.通过json来转换: In [1]: import json I ...
- python将object转换为float_DataFrame中的object转换成float的方法
DataFrame中的object转换成float的方法 数据类型转换: 今天遇到一个问题,就是DataFrame类型的数据里是str型的数字,想把数字转换为int 或float:百度没有发现好的,也 ...
- php转化xml数组_PHP实现数组array转换成xml的方法
本文实例讲述了PHP实现数组array转换成xml的方法.分享给大家供大家参考,具体如下: $elementLevel = 0 ; function array_Xml($array, $keys = ...
- 读取CSV文件内容,将其转换成JSON字符串输出
CsvToJsonUtil 工具类作用:读取CSV文件内容,将其转换成JSON字符串输出 转换工具类代码如下: package com.test.util;import java.io.*; impo ...
- 怎么将文件转换成linux文件,Linux将DOS文件格式转换成UNIX文件格式的方法
dos格式文件传输到unix系统时,会在每行的结尾多一个^M,当然也 有可能看不到,但是在vi的时候,会在下面显示此文件的格式,比如 "dos.txt"[dos] 120L, 25 ...
- 快速将Word文件转换成PPT的方法
日常办公中,Word格式的文件应该是我们使用频率最高的,平时接收文件也会遇到过PPT文件,但是PPT演示文档一般是用来做汇报的,用在各种会议中,或者是讲堂上.大家都觉得做PPT麻烦,需要填充很多内容, ...
- 视频文件转换成音频的方法
文章来源:https://www.reneelab.com.cn/convert-video-to-audio.html 目录 一.视频文件转音频格式的多种方法介绍 1.都叫兽™视频编辑软件 2.VL ...
- php把字符串变为数组_php怎么把字符串转换成数组?
php怎么把字符串转换成数组?下面本篇文章给大家介绍一下PHP把字符串转换成数组的方法.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. PHP中,将一个字符串转成数组,是使用非常频繁 ...
- php把buffer转化为图片_php base64转换成图片的方法
php base64转换成图片的方法:首先获取到前端传递的值:然后设置文件路径和命名文件名称:接着将数据流文件写入创建的文件内容中:最后将路径信息返回给前端使用即可. 将base64数据流文件转换为图 ...
最新文章
- AI金融若不解决这些问题,等于在制造新的不可解问题
- 【AOP 面向切面编程】AOP 简介 ( AspectJ 简介 | AspectJ 下载 )
- C++继承中父类和子类之间的同名覆盖
- camvid数据集介绍_fastai 官方教程之查看数据
- 走近分形与混沌(part16)--三与自组织
- Spring集成–第1节– Hello World
- Pass4side CompTIA PDI+ Beta Exam PD1-001 DEMO 免费下载
- 关于StringIndexOutOfBoundsException那些事~
- 计算机网络协议是网民们签订的合同,关于网络协议,下列__________选项是正确的。A.是网民们签订的合同B.是计算机之间的相互通信需...
- m3u8 video ios h5_移动端H5页面踩坑记
- wpf 网易云歌词_网易云音乐粉丝半年涨500万,隔壁老樊为何成今年乐坛最大黑马 | 案例池...
- unity中程序的延时
- coreldraw x4如何出血_CorelDRAW软件出血位详解
- 工字型钢弹性截面模量计算公式_截面抗弯模量的公式
- 原来这就是公文写作年终总结材料末尾段首句参考例句
- OGRE关于 Demo_Ocean 例子的分析
- (转载)程序员面试、算法研究、编程艺术、红黑树、机器学习5大系列集锦
- [交易日记]2016-11-24交易笔记
- containerd配置下载镜像
- cocos2dx在Android studio运行 以及在 Android 平台上使用 JavaScript 直接调用 Java 方法
热门文章
- WINCE注册表应用
- 1.7 理解 Dropout-深度学习第二课《改善深层神经网络》-Stanford吴恩达教授
- arm交叉编译器gnueabi、none-eabi、arm-eabi、gnueabihf的区别
- 物联网云平台-贝壳物联入门详细使用方法
- Spring Cloud构建微服务架构:服务容错保护(Hystrix断路器)
- 自研服务治理框架----文章汇总
- 管理信息系统的开发和管理
- Java_JDBC_MySql
- Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用
- 理解SQL Server中的锁