1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (c) 2003 Jose Solorzano. All rights reserved. |
5
|
|
|
* Redistribution of source must retain this copyright notice. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
include ("htmlparser.inc"); |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Html2Text. (HtmlParser example.) |
12
|
|
|
* Converts HTML to ASCII attempting to preserve |
13
|
|
|
* document structure. |
14
|
|
|
* To use, create an instance of Html2Text passing |
15
|
|
|
* the text to convert and the desired maximum |
16
|
|
|
* number of characters per line. Then invoke |
17
|
|
|
* convert() which returns ASCII text. |
18
|
|
|
*/ |
19
|
|
|
class Html2Text { |
20
|
|
|
|
21
|
|
|
// Private fields |
22
|
|
|
|
23
|
|
|
var $iCurrentLine = ""; |
24
|
|
|
var $iCurrentWord = ""; |
25
|
|
|
var $iCurrentWordArray; |
26
|
|
|
var $iCurrentWordIndex; |
27
|
|
|
var $iInScript; |
28
|
|
|
var $iListLevel = 0; |
29
|
|
|
var $iHtmlText; |
30
|
|
|
var $iMaxColumns; |
31
|
|
|
var $iHtmlParser; |
32
|
|
|
|
33
|
|
|
// Constants |
34
|
|
|
|
35
|
|
|
var $TOKEN_BR = 0; |
36
|
|
|
var $TOKEN_P = 1; |
37
|
|
|
var $TOKEN_LI = 2; |
38
|
|
|
var $TOKEN_AFTERLI = 3; |
39
|
|
|
var $TOKEN_UL = 4; |
40
|
|
|
var $TOKEN_ENDUL = 5; |
41
|
|
|
|
42
|
|
|
function __construct ($aHtmlText, $aMaxColumns) { |
43
|
|
|
$this->iHtmlText = $aHtmlText; |
44
|
|
|
$this->iMaxColumns = $aMaxColumns; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
function convert() { |
48
|
|
|
$this->iHtmlParser = new h2tHtmlParser($this->iHtmlText); |
49
|
|
|
$wholeText = ""; |
50
|
|
|
while (($line = $this->getLine()) !== false) { |
51
|
|
|
$wholeText .= ($line . "\r\n"); |
52
|
|
|
} |
53
|
|
|
return $wholeText; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
function getLine() { |
57
|
|
|
while (true) { |
58
|
|
|
if (!$this->addWordToLine($this->iCurrentWord)) { |
59
|
|
|
$retvalue = $this->iCurrentLine; |
60
|
|
|
$this->iCurrentLine = ""; |
61
|
|
|
return $retvalue; |
62
|
|
|
} |
63
|
|
|
$word = $this->getWord(); |
64
|
|
|
if ($word === false) { |
65
|
|
|
if ($this->iCurrentLine == "") { |
66
|
|
|
break; |
67
|
|
|
} |
68
|
|
|
$retvalue = $this->iCurrentLine; |
69
|
|
|
$this->iCurrentLine = ""; |
70
|
|
|
$this->iInText = false; |
|
|
|
|
71
|
|
|
$this->iCurrentWord = ""; |
72
|
|
|
return $retvalue; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
function addWordToLine ($word) { |
79
|
|
|
if ($this->iInScript) { |
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
$prevLine = $this->iCurrentLine; |
83
|
|
|
if ($word === $this->TOKEN_BR) { |
84
|
|
|
$this->iCurrentWord = ""; |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
if ($word === $this->TOKEN_P) { |
88
|
|
|
$this->iCurrentWord = $this->TOKEN_BR; |
|
|
|
|
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
if ($word === $this->TOKEN_UL) { |
92
|
|
|
$this->iCurrentWord = $this->TOKEN_BR; |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
if ($word === $this->TOKEN_ENDUL) { |
96
|
|
|
$this->iCurrentWord = $this->TOKEN_BR; |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
if ($word === $this->TOKEN_LI) { |
100
|
|
|
$this->iCurrentWord = $this->TOKEN_AFTERLI; |
|
|
|
|
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
$toAdd = $word; |
104
|
|
|
if ($word === $this->TOKEN_AFTERLI) { |
105
|
|
|
$toAdd = ""; |
106
|
|
|
} |
107
|
|
|
if ($prevLine != "") { |
108
|
|
|
$prevLine .= " "; |
109
|
|
|
} |
110
|
|
|
else { |
111
|
|
|
$prevLine = $this->getIndentation($word === $this->TOKEN_AFTERLI); |
112
|
|
|
} |
113
|
|
|
$candidateLine = $prevLine . $toAdd; |
114
|
|
|
if (strlen ($candidateLine) > $this->iMaxColumns && $prevLine != "") { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
$this->iCurrentLine = $candidateLine; |
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
function getWord() { |
122
|
|
|
while (true) { |
123
|
|
|
if ($this->iHtmlParser->iNodeType == NODE_TYPE_TEXT) { |
|
|
|
|
124
|
|
|
if (!$this->iInText) { |
125
|
|
|
$words = $this->splitWords($this->iHtmlParser->iNodeValue); |
|
|
|
|
126
|
|
|
$this->iCurrentWordArray = $words; |
127
|
|
|
$this->iCurrentWordIndex = 0; |
128
|
|
|
$this->iInText = true; |
129
|
|
|
} |
130
|
|
|
if ($this->iCurrentWordIndex < count($this->iCurrentWordArray)) { |
131
|
|
|
$this->iCurrentWord = $this->iCurrentWordArray[$this->iCurrentWordIndex++]; |
132
|
|
|
return $this->iCurrentWord; |
133
|
|
|
} |
134
|
|
|
else { |
135
|
|
|
$this->iInText = false; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
else if ($this->iHtmlParser->iNodeType == NODE_TYPE_ELEMENT) { |
|
|
|
|
139
|
|
|
if (strcasecmp ($this->iHtmlParser->iNodeName, "br") == 0) { |
|
|
|
|
140
|
|
|
$this->iHtmlParser->parse(); |
141
|
|
|
$this->iCurrentWord = $this->TOKEN_BR; |
|
|
|
|
142
|
|
|
return $this->iCurrentWord; |
143
|
|
|
} |
144
|
|
|
else if (strcasecmp ($this->iHtmlParser->iNodeName, "p") == 0) { |
|
|
|
|
145
|
|
|
$this->iHtmlParser->parse(); |
146
|
|
|
$this->iCurrentWord = $this->TOKEN_P; |
|
|
|
|
147
|
|
|
return $this->iCurrentWord; |
148
|
|
|
} |
149
|
|
|
else if (strcasecmp ($this->iHtmlParser->iNodeName, "script") == 0) { |
|
|
|
|
150
|
|
|
$this->iHtmlParser->parse(); |
151
|
|
|
$this->iCurrentWord = ""; |
152
|
|
|
$this->iInScript = true; |
153
|
|
|
return $this->iCurrentWord; |
154
|
|
|
} |
155
|
|
|
else if (strcasecmp ($this->iHtmlParser->iNodeName, "ul") == 0 || strcasecmp ($this->iHtmlParser->iNodeName, "ol") == 0) { |
|
|
|
|
156
|
|
|
$this->iHtmlParser->parse(); |
157
|
|
|
$this->iCurrentWord = $this->TOKEN_UL; |
|
|
|
|
158
|
|
|
$this->iListLevel++; |
159
|
|
|
return $this->iCurrentWord; |
160
|
|
|
} |
161
|
|
|
else if (strcasecmp ($this->iHtmlParser->iNodeName, "li") == 0) { |
|
|
|
|
162
|
|
|
$this->iHtmlParser->parse(); |
163
|
|
|
$this->iCurrentWord = $this->TOKEN_LI; |
|
|
|
|
164
|
|
|
return $this->iCurrentWord; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
else if ($this->iHtmlParser->iNodeType == NODE_TYPE_ENDELEMENT) { |
|
|
|
|
168
|
|
|
if (strcasecmp ($this->iHtmlParser->iNodeName, "script") == 0) { |
|
|
|
|
169
|
|
|
$this->iHtmlParser->parse(); |
170
|
|
|
$this->iCurrentWord = ""; |
171
|
|
|
$this->iInScript = false; |
172
|
|
|
return $this->iCurrentWord; |
173
|
|
|
} |
174
|
|
|
else if (strcasecmp ($this->iHtmlParser->iNodeName, "ul") == 0 || strcasecmp ($this->iHtmlParser->iNodeName, "ol") == 0) { |
|
|
|
|
175
|
|
|
$this->iHtmlParser->parse(); |
176
|
|
|
$this->iCurrentWord = $this->TOKEN_ENDUL; |
|
|
|
|
177
|
|
|
if ($this->iListLevel > 0) { |
178
|
|
|
$this->iListLevel--; |
179
|
|
|
} |
180
|
|
|
return $this->iCurrentWord; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
if (!$this->iHtmlParser->parse()) { |
184
|
|
|
break; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
function splitWords ($text) { |
191
|
|
|
$words = preg_split ("/[ \t\r\n]+/", $text); |
192
|
|
|
for ($idx = 0; $idx < count($words); $idx++) { |
|
|
|
|
193
|
|
|
$words[$idx] = $this->htmlDecode($words[$idx]); |
194
|
|
|
} |
195
|
|
|
return $words; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
function htmlDecode ($text) { |
199
|
|
|
$srcTable = Array(' ', '&', '<', '>', '"e;'); |
200
|
|
|
$dstTable = Array(' ', '&', '<', '>', '"'); |
201
|
|
|
return str_replace($srcTable, $dstTable, $text); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
function getIndentation ($hasLI) { |
205
|
|
|
$indent = ""; |
206
|
|
|
$idx = 0; |
|
|
|
|
207
|
|
|
for ($idx = 0; $idx < ($this->iListLevel - 1); $idx++) { |
208
|
|
|
$indent .= " "; |
209
|
|
|
} |
210
|
|
|
if ($this->iListLevel > 0) { |
211
|
|
|
$indent = $hasLI ? ($indent . "- ") : ($indent . " "); |
212
|
|
|
} |
213
|
|
|
return $indent; |
214
|
|
|
} |
215
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: