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

html">php解析mht文件,使用编辑器打开可以看到base64编码所以,mht是可以转换成html的。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

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

本文链接:https://my.lmcjl.com/post/18998.html

展开阅读全文

4 评论

留下您的评论.