commit | author | age
|
4e17e6
|
1 |
<?php |
T |
2 |
|
|
3 |
/************************************************************************* |
8ac6fd
|
4 |
* * |
A |
5 |
* class.html2text.inc * |
|
6 |
* * |
|
7 |
************************************************************************* |
|
8 |
* * |
|
9 |
* Converts HTML to formatted plain text * |
|
10 |
* * |
|
11 |
* Copyright (c) 2005-2007 Jon Abernathy <jon@chuggnutt.com> * |
|
12 |
* All rights reserved. * |
|
13 |
* * |
|
14 |
* This script is free software; you can redistribute it and/or modify * |
|
15 |
* it under the terms of the GNU General Public License as published by * |
|
16 |
* the Free Software Foundation; either version 2 of the License, or * |
|
17 |
* (at your option) any later version. * |
|
18 |
* * |
|
19 |
* The GNU General Public License can be found at * |
|
20 |
* http://www.gnu.org/copyleft/gpl.html. * |
|
21 |
* * |
|
22 |
* This script is distributed in the hope that it will be useful, * |
|
23 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|
24 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|
25 |
* GNU General Public License for more details. * |
|
26 |
* * |
|
27 |
* Author(s): Jon Abernathy <jon@chuggnutt.com> * |
|
28 |
* * |
|
29 |
* Last modified: 08/08/07 * |
|
30 |
* * |
|
31 |
*************************************************************************/ |
4e17e6
|
32 |
|
T |
33 |
|
|
34 |
/** |
8ac6fd
|
35 |
* Takes HTML and converts it to formatted, plain text. |
A |
36 |
* |
|
37 |
* Thanks to Alexander Krug (http://www.krugar.de/) to pointing out and |
|
38 |
* correcting an error in the regexp search array. Fixed 7/30/03. |
|
39 |
* |
|
40 |
* Updated set_html() function's file reading mechanism, 9/25/03. |
|
41 |
* |
|
42 |
* Thanks to Joss Sanglier (http://www.dancingbear.co.uk/) for adding |
|
43 |
* several more HTML entity codes to the $search and $replace arrays. |
|
44 |
* Updated 11/7/03. |
|
45 |
* |
|
46 |
* Thanks to Darius Kasperavicius (http://www.dar.dar.lt/) for |
|
47 |
* suggesting the addition of $allowed_tags and its supporting function |
|
48 |
* (which I slightly modified). Updated 3/12/04. |
|
49 |
* |
|
50 |
* Thanks to Justin Dearing for pointing out that a replacement for the |
|
51 |
* <TH> tag was missing, and suggesting an appropriate fix. |
|
52 |
* Updated 8/25/04. |
|
53 |
* |
|
54 |
* Thanks to Mathieu Collas (http://www.myefarm.com/) for finding a |
|
55 |
* display/formatting bug in the _build_link_list() function: email |
|
56 |
* readers would show the left bracket and number ("[1") as part of the |
|
57 |
* rendered email address. |
|
58 |
* Updated 12/16/04. |
|
59 |
* |
|
60 |
* Thanks to Wojciech Bajon (http://histeria.pl/) for submitting code |
|
61 |
* to handle relative links, which I hadn't considered. I modified his |
|
62 |
* code a bit to handle normal HTTP links and MAILTO links. Also for |
|
63 |
* suggesting three additional HTML entity codes to search for. |
|
64 |
* Updated 03/02/05. |
|
65 |
* |
|
66 |
* Thanks to Jacob Chandler for pointing out another link condition |
|
67 |
* for the _build_link_list() function: "https". |
|
68 |
* Updated 04/06/05. |
|
69 |
* |
|
70 |
* Thanks to Marc Bertrand (http://www.dresdensky.com/) for |
|
71 |
* suggesting a revision to the word wrapping functionality; if you |
|
72 |
* specify a $width of 0 or less, word wrapping will be ignored. |
|
73 |
* Updated 11/02/06. |
|
74 |
* |
|
75 |
* *** Big housecleaning updates below: |
|
76 |
* |
|
77 |
* Thanks to Colin Brown (http://www.sparkdriver.co.uk/) for |
|
78 |
* suggesting the fix to handle </li> and blank lines (whitespace). |
|
79 |
* Christian Basedau (http://www.movetheweb.de/) also suggested the |
|
80 |
* blank lines fix. |
|
81 |
* |
|
82 |
* Special thanks to Marcus Bointon (http://www.synchromedia.co.uk/), |
|
83 |
* Christian Basedau, Norbert Laposa (http://ln5.co.uk/), |
|
84 |
* Bas van de Weijer, and Marijn van Butselaar |
|
85 |
* for pointing out my glaring error in the <th> handling. Marcus also |
|
86 |
* supplied a host of fixes. |
|
87 |
* |
|
88 |
* Thanks to Jeffrey Silverman (http://www.newtnotes.com/) for pointing |
|
89 |
* out that extra spaces should be compressed--a problem addressed with |
|
90 |
* Marcus Bointon's fixes but that I had not yet incorporated. |
|
91 |
* |
|
92 |
* Thanks to Daniel Schledermann (http://www.typoconsult.dk/) for |
|
93 |
* suggesting a valuable fix with <a> tag handling. |
|
94 |
* |
|
95 |
* Thanks to Wojciech Bajon (again!) for suggesting fixes and additions, |
|
96 |
* including the <a> tag handling that Daniel Schledermann pointed |
|
97 |
* out but that I had not yet incorporated. I haven't (yet) |
|
98 |
* incorporated all of Wojciech's changes, though I may at some |
|
99 |
* future time. |
|
100 |
* |
|
101 |
* *** End of the housecleaning updates. Updated 08/08/07. |
|
102 |
* |
|
103 |
* @author Jon Abernathy <jon@chuggnutt.com> |
|
104 |
* @version 1.0.0 |
|
105 |
* @since PHP 4.0.2 |
|
106 |
*/ |
4e17e6
|
107 |
class html2text |
T |
108 |
{ |
|
109 |
|
|
110 |
/** |
|
111 |
* Contains the HTML content to convert. |
|
112 |
* |
|
113 |
* @var string $html |
|
114 |
* @access public |
|
115 |
*/ |
|
116 |
var $html; |
|
117 |
|
|
118 |
/** |
|
119 |
* Contains the converted, formatted text. |
|
120 |
* |
|
121 |
* @var string $text |
|
122 |
* @access public |
|
123 |
*/ |
|
124 |
var $text; |
|
125 |
|
|
126 |
/** |
|
127 |
* Maximum width of the formatted text, in columns. |
|
128 |
* |
8ac6fd
|
129 |
* Set this value to 0 (or less) to ignore word wrapping |
A |
130 |
* and not constrain text to a fixed-width column. |
|
131 |
* |
4e17e6
|
132 |
* @var integer $width |
T |
133 |
* @access public |
|
134 |
*/ |
|
135 |
var $width = 70; |
|
136 |
|
|
137 |
/** |
|
138 |
* List of preg* regular expression patterns to search for, |
|
139 |
* used in conjunction with $replace. |
|
140 |
* |
|
141 |
* @var array $search |
|
142 |
* @access public |
|
143 |
* @see $replace |
|
144 |
*/ |
|
145 |
var $search = array( |
|
146 |
"/\r/", // Non-legal carriage return |
|
147 |
"/[\n\t]+/", // Newlines and tabs |
8ac6fd
|
148 |
'/[ ]{2,}/', // Runs of spaces, pre-handling |
4e17e6
|
149 |
'/<script[^>]*>.*?<\/script>/i', // <script>s -- which strip_tags supposedly has problems with |
8ac6fd
|
150 |
'/<style[^>]*>.*?<\/style>/i', // <style>s -- which strip_tags supposedly has problems with |
4e17e6
|
151 |
'/<p[^>]*>/i', // <P> |
T |
152 |
'/<br[^>]*>/i', // <br> |
8ac6fd
|
153 |
'/<i[^>]*>(.*?)<\/i>/i', // <i> |
A |
154 |
'/<em[^>]*>(.*?)<\/em>/i', // <em> |
4e17e6
|
155 |
'/(<ul[^>]*>|<\/ul>)/i', // <ul> and </ul> |
T |
156 |
'/(<ol[^>]*>|<\/ol>)/i', // <ol> and </ol> |
8ac6fd
|
157 |
'/<li[^>]*>(.*?)<\/li>/i', // <li> and </li> |
4e17e6
|
158 |
'/<li[^>]*>/i', // <li> |
T |
159 |
'/<hr[^>]*>/i', // <hr> |
f6b282
|
160 |
'/<div[^>]*>/i', // <div> |
4e17e6
|
161 |
'/(<table[^>]*>|<\/table>)/i', // <table> and </table> |
T |
162 |
'/(<tr[^>]*>|<\/tr>)/i', // <tr> and </tr> |
8ac6fd
|
163 |
'/<td[^>]*>(.*?)<\/td>/i', // <td> and </td> |
A |
164 |
'/&(nbsp|#160);/i', // Non-breaking space |
|
165 |
'/&(quot|rdquo|ldquo|#8220|#8221|#147|#148);/i', |
|
166 |
// Double quotes |
|
167 |
'/&(apos|rsquo|lsquo|#8216|#8217);/i', // Single quotes |
|
168 |
'/>/i', // Greater-than |
|
169 |
'/</i', // Less-than |
|
170 |
'/&(copy|#169);/i', // Copyright |
|
171 |
'/&(trade|#8482|#153);/i', // Trademark |
|
172 |
'/&(reg|#174);/i', // Registered |
|
173 |
'/&(mdash|#151|#8212);/i', // mdash |
|
174 |
'/&(ndash|minus|#8211|#8722);/i', // ndash |
|
175 |
'/&(bull|#149|#8226);/i', // Bullet |
|
176 |
'/&(pound|#163);/i', // Pound sign |
|
177 |
'/&(euro|#8364);/i', // Euro sign |
6084d7
|
178 |
'/&(amp|#38);/i', // Ampersand: see _converter() |
8ac6fd
|
179 |
'/[ ]{2,}/' // Runs of spaces, post-handling |
4e17e6
|
180 |
); |
T |
181 |
|
|
182 |
/** |
|
183 |
* List of pattern replacements corresponding to patterns searched. |
|
184 |
* |
|
185 |
* @var array $replace |
|
186 |
* @access public |
|
187 |
* @see $search |
|
188 |
*/ |
|
189 |
var $replace = array( |
|
190 |
'', // Non-legal carriage return |
|
191 |
' ', // Newlines and tabs |
8ac6fd
|
192 |
' ', // Runs of spaces, pre-handling |
4e17e6
|
193 |
'', // <script>s -- which strip_tags supposedly has problems with |
8ac6fd
|
194 |
'', // <style>s -- which strip_tags supposedly has problems with |
f6b282
|
195 |
"\n\n", // <P> |
4e17e6
|
196 |
"\n", // <br> |
T |
197 |
'_\\1_', // <i> |
8ac6fd
|
198 |
'_\\1_', // <em> |
4e17e6
|
199 |
"\n\n", // <ul> and </ul> |
T |
200 |
"\n\n", // <ol> and </ol> |
8ac6fd
|
201 |
"\t* \\1\n", // <li> and </li> |
A |
202 |
"\n\t* ", // <li> |
6972cc
|
203 |
"\n-------------------------\n", // <hr> |
11bcac
|
204 |
"<div>\n", // <div> |
6972cc
|
205 |
"\n\n", // <table> and </table> |
4e17e6
|
206 |
"\n", // <tr> and </tr> |
T |
207 |
"\t\t\\1\n", // <td> and </td> |
8ac6fd
|
208 |
' ', // Non-breaking space |
A |
209 |
'"', // Double quotes |
|
210 |
"'", // Single quotes |
4e17e6
|
211 |
'>', |
T |
212 |
'<', |
|
213 |
'(c)', |
|
214 |
'(tm)', |
|
215 |
'(R)', |
8ac6fd
|
216 |
'--', |
A |
217 |
'-', |
4e17e6
|
218 |
'*', |
300fc6
|
219 |
'£', |
8ac6fd
|
220 |
'EUR', // Euro sign. € ? |
6084d7
|
221 |
'|+|amp|+|', // Ampersand: see _converter() |
8ac6fd
|
222 |
' ' // Runs of spaces, post-handling |
f50cc7
|
223 |
); |
A |
224 |
|
|
225 |
/** |
|
226 |
* List of preg* regular expression patterns to search for |
|
227 |
* and replace using callback function. |
|
228 |
* |
|
229 |
* @var array $callback_search |
|
230 |
* @access public |
|
231 |
*/ |
|
232 |
var $callback_search = array( |
|
233 |
'/<(h)[123456][^>]*>(.*?)<\/h[123456]>/i', // H1 - H3 |
|
234 |
'/<(b)[^>]*>(.*?)<\/b>/i', // <b> |
|
235 |
'/<(strong)[^>]*>(.*?)<\/strong>/i', // <strong> |
|
236 |
'/<(a) [^>]*href=("|\')([^"\']+)\2[^>]*>(.*?)<\/a>/i', |
|
237 |
// <a href=""> |
|
238 |
'/<(th)[^>]*>(.*?)<\/th>/i', // <th> and </th> |
7353fa
|
239 |
); |
A |
240 |
|
8ac6fd
|
241 |
/** |
A |
242 |
* List of preg* regular expression patterns to search for in PRE body, |
|
243 |
* used in conjunction with $pre_replace. |
|
244 |
* |
|
245 |
* @var array $pre_search |
|
246 |
* @access public |
|
247 |
* @see $pre_replace |
|
248 |
*/ |
7789fd
|
249 |
var $pre_search = array( |
6972cc
|
250 |
"/\n/", |
T |
251 |
"/\t/", |
|
252 |
'/ /', |
|
253 |
'/<pre[^>]*>/', |
|
254 |
'/<\/pre>/' |
7353fa
|
255 |
); |
A |
256 |
|
|
257 |
/** |
|
258 |
* List of pattern replacements corresponding to patterns searched for PRE body. |
|
259 |
* |
|
260 |
* @var array $pre_replace |
|
261 |
* @access public |
|
262 |
* @see $pre_search |
|
263 |
*/ |
7789fd
|
264 |
var $pre_replace = array( |
6972cc
|
265 |
'<br>', |
T |
266 |
' ', |
|
267 |
' ', |
|
268 |
'', |
|
269 |
'' |
4e17e6
|
270 |
); |
T |
271 |
|
|
272 |
/** |
|
273 |
* Contains a list of HTML tags to allow in the resulting text. |
|
274 |
* |
|
275 |
* @var string $allowed_tags |
|
276 |
* @access public |
|
277 |
* @see set_allowed_tags() |
|
278 |
*/ |
|
279 |
var $allowed_tags = ''; |
|
280 |
|
|
281 |
/** |
|
282 |
* Contains the base URL that relative links should resolve to. |
|
283 |
* |
|
284 |
* @var string $url |
|
285 |
* @access public |
|
286 |
*/ |
|
287 |
var $url; |
|
288 |
|
|
289 |
/** |
|
290 |
* Indicates whether content in the $html variable has been converted yet. |
|
291 |
* |
8ac6fd
|
292 |
* @var boolean $_converted |
4e17e6
|
293 |
* @access private |
T |
294 |
* @see $html, $text |
|
295 |
*/ |
|
296 |
var $_converted = false; |
|
297 |
|
|
298 |
/** |
|
299 |
* Contains URL addresses from links to be rendered in plain text. |
|
300 |
* |
8ac6fd
|
301 |
* @var string $_link_list |
4e17e6
|
302 |
* @access private |
T |
303 |
* @see _build_link_list() |
|
304 |
*/ |
8ac6fd
|
305 |
var $_link_list = ''; |
dd792e
|
306 |
|
S |
307 |
/** |
8ac6fd
|
308 |
* Number of valid links detected in the text, used for plain text |
A |
309 |
* display (rendered similar to footnotes). |
|
310 |
* |
|
311 |
* @var integer $_link_count |
|
312 |
* @access private |
|
313 |
* @see _build_link_list() |
dd792e
|
314 |
*/ |
8ac6fd
|
315 |
var $_link_count = 0; |
4e17e6
|
316 |
|
e7f85b
|
317 |
/** |
A |
318 |
* Boolean flag, true if a table of link URLs should be listed after the text. |
|
319 |
* |
|
320 |
* @var boolean $_do_links |
|
321 |
* @access private |
|
322 |
* @see html2text() |
|
323 |
*/ |
|
324 |
var $_do_links = true; |
|
325 |
|
4e17e6
|
326 |
/** |
T |
327 |
* Constructor. |
|
328 |
* |
|
329 |
* If the HTML source string (or file) is supplied, the class |
|
330 |
* will instantiate with that source propagated, all that has |
|
331 |
* to be done it to call get_text(). |
|
332 |
* |
|
333 |
* @param string $source HTML content |
|
334 |
* @param boolean $from_file Indicates $source is a file to pull content from |
e7f85b
|
335 |
* @param boolean $do_links Indicate whether a table of link URLs is desired |
A |
336 |
* @param integer $width Maximum width of the formatted text, 0 for no limit |
4e17e6
|
337 |
* @access public |
T |
338 |
* @return void |
|
339 |
*/ |
e7f85b
|
340 |
function html2text( $source = '', $from_file = false, $do_links = true, $width = 75 ) |
4e17e6
|
341 |
{ |
T |
342 |
if ( !empty($source) ) { |
|
343 |
$this->set_html($source, $from_file); |
|
344 |
} |
6972cc
|
345 |
|
4e17e6
|
346 |
$this->set_base_url(); |
6972cc
|
347 |
$this->_do_links = $do_links; |
T |
348 |
$this->width = $width; |
4e17e6
|
349 |
} |
T |
350 |
|
|
351 |
/** |
|
352 |
* Loads source HTML into memory, either from $source string or a file. |
|
353 |
* |
|
354 |
* @param string $source HTML content |
|
355 |
* @param boolean $from_file Indicates $source is a file to pull content from |
|
356 |
* @access public |
|
357 |
* @return void |
|
358 |
*/ |
|
359 |
function set_html( $source, $from_file = false ) |
|
360 |
{ |
|
361 |
if ( $from_file && file_exists($source) ) { |
6972cc
|
362 |
$this->html = file_get_contents($source); |
4e17e6
|
363 |
} |
8ac6fd
|
364 |
else |
6972cc
|
365 |
$this->html = $source; |
4e17e6
|
366 |
|
T |
367 |
$this->_converted = false; |
|
368 |
} |
|
369 |
|
|
370 |
/** |
|
371 |
* Returns the text, converted from HTML. |
|
372 |
* |
|
373 |
* @access public |
|
374 |
* @return string |
|
375 |
*/ |
|
376 |
function get_text() |
|
377 |
{ |
|
378 |
if ( !$this->_converted ) { |
|
379 |
$this->_convert(); |
|
380 |
} |
|
381 |
|
|
382 |
return $this->text; |
|
383 |
} |
|
384 |
|
|
385 |
/** |
|
386 |
* Prints the text, converted from HTML. |
|
387 |
* |
|
388 |
* @access public |
|
389 |
* @return void |
|
390 |
*/ |
|
391 |
function print_text() |
|
392 |
{ |
|
393 |
print $this->get_text(); |
|
394 |
} |
|
395 |
|
|
396 |
/** |
|
397 |
* Alias to print_text(), operates identically. |
|
398 |
* |
|
399 |
* @access public |
|
400 |
* @return void |
|
401 |
* @see print_text() |
|
402 |
*/ |
|
403 |
function p() |
|
404 |
{ |
|
405 |
print $this->get_text(); |
|
406 |
} |
|
407 |
|
|
408 |
/** |
|
409 |
* Sets the allowed HTML tags to pass through to the resulting text. |
|
410 |
* |
|
411 |
* Tags should be in the form "<p>", with no corresponding closing tag. |
|
412 |
* |
|
413 |
* @access public |
|
414 |
* @return void |
|
415 |
*/ |
|
416 |
function set_allowed_tags( $allowed_tags = '' ) |
|
417 |
{ |
|
418 |
if ( !empty($allowed_tags) ) { |
|
419 |
$this->allowed_tags = $allowed_tags; |
|
420 |
} |
|
421 |
} |
|
422 |
|
|
423 |
/** |
|
424 |
* Sets a base URL to handle relative links. |
|
425 |
* |
|
426 |
* @access public |
|
427 |
* @return void |
|
428 |
*/ |
|
429 |
function set_base_url( $url = '' ) |
|
430 |
{ |
|
431 |
if ( empty($url) ) { |
8ac6fd
|
432 |
if ( !empty($_SERVER['HTTP_HOST']) ) { |
A |
433 |
$this->url = 'http://' . $_SERVER['HTTP_HOST']; |
|
434 |
} else { |
|
435 |
$this->url = ''; |
|
436 |
} |
4e17e6
|
437 |
} else { |
T |
438 |
// Strip any trailing slashes for consistency (relative |
|
439 |
// URLs may already start with a slash like "/file.html") |
|
440 |
if ( substr($url, -1) == '/' ) { |
|
441 |
$url = substr($url, 0, -1); |
|
442 |
} |
|
443 |
$this->url = $url; |
|
444 |
} |
|
445 |
} |
|
446 |
|
|
447 |
/** |
11bcac
|
448 |
* Workhorse function that does actual conversion (calls _converter() method). |
4e17e6
|
449 |
* |
T |
450 |
* @access private |
|
451 |
* @return void |
|
452 |
*/ |
|
453 |
function _convert() |
|
454 |
{ |
|
455 |
// Variables used for building the link list |
8ac6fd
|
456 |
$this->_link_count = 0; |
A |
457 |
$this->_link_list = ''; |
4e17e6
|
458 |
|
T |
459 |
$text = trim(stripslashes($this->html)); |
11bcac
|
460 |
|
A |
461 |
// Convert HTML to TXT |
|
462 |
$this->_converter($text); |
|
463 |
|
|
464 |
// Add link list |
|
465 |
if ( !empty($this->_link_list) ) { |
|
466 |
$text .= "\n\nLinks:\n------\n" . $this->_link_list; |
|
467 |
} |
|
468 |
|
|
469 |
$this->text = $text; |
|
470 |
|
|
471 |
$this->_converted = true; |
|
472 |
} |
|
473 |
|
|
474 |
/** |
|
475 |
* Workhorse function that does actual conversion. |
|
476 |
* |
|
477 |
* First performs custom tag replacement specified by $search and |
|
478 |
* $replace arrays. Then strips any remaining HTML tags, reduces whitespace |
|
479 |
* and newlines to a readable format, and word wraps the text to |
|
480 |
* $width characters. |
|
481 |
* |
|
482 |
* @param string Reference to HTML content string |
|
483 |
* |
|
484 |
* @access private |
|
485 |
* @return void |
|
486 |
*/ |
|
487 |
function _converter(&$text) |
|
488 |
{ |
|
489 |
// Convert <BLOCKQUOTE> (before PRE!) |
|
490 |
$this->_convert_blockquotes($text); |
4e17e6
|
491 |
|
6972cc
|
492 |
// Convert <PRE> |
8ac6fd
|
493 |
$this->_convert_pre($text); |
300fc6
|
494 |
|
4e17e6
|
495 |
// Run our defined search-and-replace |
T |
496 |
$text = preg_replace($this->search, $this->replace, $text); |
|
497 |
|
6972cc
|
498 |
// Replace known html entities |
T |
499 |
$text = html_entity_decode($text, ENT_COMPAT, 'UTF-8'); |
da1722
|
500 |
|
A |
501 |
// Run our defined search-and-replace with callback |
|
502 |
$text = preg_replace_callback($this->callback_search, array('html2text', '_preg_callback'), $text); |
4e0419
|
503 |
|
755900
|
504 |
// Remove unknown/unhandled entities (this cannot be done in search-and-replace block) |
6084d7
|
505 |
$text = preg_replace('/&([a-zA-Z0-9]{2,6}|#[0-9]{2,4});/', '', $text); |
A |
506 |
|
|
507 |
// Convert "|+|amp|+|" into "&", need to be done after handling of unknown entities |
|
508 |
// This properly handles situation of "&quot;" in input string |
|
509 |
$text = str_replace('|+|amp|+|', '&', $text); |
755900
|
510 |
|
4e17e6
|
511 |
// Strip any other HTML tags |
T |
512 |
$text = strip_tags($text, $this->allowed_tags); |
|
513 |
|
|
514 |
// Bring down number of empty lines to 2 max |
8ac6fd
|
515 |
$text = preg_replace("/\n\s+\n/", "\n\n", $text); |
4e17e6
|
516 |
$text = preg_replace("/[\n]{3,}/", "\n\n", $text); |
T |
517 |
|
4d7fbd
|
518 |
// remove leading empty lines (can be produced by eg. P tag on the beginning) |
A |
519 |
$text = preg_replace('/^\n+/', '', $text); |
|
520 |
|
4e17e6
|
521 |
// Wrap the text to a readable format |
T |
522 |
// for PHP versions >= 4.0.2. Default width is 75 |
8ac6fd
|
523 |
// If width is 0 or less, don't wrap the text. |
A |
524 |
if ( $this->width > 0 ) { |
|
525 |
$text = wordwrap($text, $this->width); |
|
526 |
} |
4e17e6
|
527 |
} |
T |
528 |
|
|
529 |
/** |
|
530 |
* Helper function called by preg_replace() on link replacement. |
|
531 |
* |
|
532 |
* Maintains an internal list of links to be displayed at the end of the |
|
533 |
* text, with numeric indices to the original point in the text they |
|
534 |
* appeared. Also makes an effort at identifying and handling absolute |
|
535 |
* and relative links. |
|
536 |
* |
|
537 |
* @param string $link URL of the link |
|
538 |
* @param string $display Part of the text to associate number with |
|
539 |
* @access private |
|
540 |
* @return string |
8ac6fd
|
541 |
*/ |
A |
542 |
function _build_link_list( $link, $display ) |
|
543 |
{ |
11bcac
|
544 |
if ( !$this->_do_links ) |
A |
545 |
return $display; |
|
546 |
|
|
547 |
if ( substr($link, 0, 7) == 'http://' || substr($link, 0, 8) == 'https://' || |
|
548 |
substr($link, 0, 7) == 'mailto:' |
|
549 |
) { |
8ac6fd
|
550 |
$this->_link_count++; |
11bcac
|
551 |
$this->_link_list .= '[' . $this->_link_count . "] $link\n"; |
8ac6fd
|
552 |
$additional = ' [' . $this->_link_count . ']'; |
11bcac
|
553 |
} elseif ( substr($link, 0, 11) == 'javascript:' ) { |
A |
554 |
// Don't count the link; ignore it |
|
555 |
$additional = ''; |
8ac6fd
|
556 |
// what about href="#anchor" ? |
A |
557 |
} else { |
|
558 |
$this->_link_count++; |
11bcac
|
559 |
$this->_link_list .= '[' . $this->_link_count . '] ' . $this->url; |
8ac6fd
|
560 |
if ( substr($link, 0, 1) != '/' ) { |
A |
561 |
$this->_link_list .= '/'; |
4e17e6
|
562 |
} |
8ac6fd
|
563 |
$this->_link_list .= "$link\n"; |
A |
564 |
$additional = ' [' . $this->_link_count . ']'; |
4e17e6
|
565 |
} |
T |
566 |
|
8ac6fd
|
567 |
return $display . $additional; |
A |
568 |
} |
11bcac
|
569 |
|
7353fa
|
570 |
/** |
A |
571 |
* Helper function for PRE body conversion. |
|
572 |
* |
|
573 |
* @param string HTML content |
|
574 |
* @access private |
8ac6fd
|
575 |
*/ |
7353fa
|
576 |
function _convert_pre(&$text) |
8ac6fd
|
577 |
{ |
d483cd
|
578 |
// get the content of PRE element |
11bcac
|
579 |
while (preg_match('/<pre[^>]*>(.*)<\/pre>/ismU', $text, $matches)) { |
d483cd
|
580 |
// convert the content |
A |
581 |
$this->pre_content = sprintf('<div><br>%s<br></div>', |
|
582 |
preg_replace($this->pre_search, $this->pre_replace, $matches[1])); |
|
583 |
// replace the content (use callback because content can contain $0 variable) |
|
584 |
$text = preg_replace_callback('/<pre[^>]*>.*<\/pre>/ismU', |
|
585 |
array('html2text', '_preg_pre_callback'), $text, 1); |
|
586 |
// free memory |
|
587 |
$this->pre_content = ''; |
11bcac
|
588 |
} |
A |
589 |
} |
|
590 |
|
|
591 |
/** |
|
592 |
* Helper function for BLOCKQUOTE body conversion. |
|
593 |
* |
|
594 |
* @param string HTML content |
|
595 |
* @access private |
|
596 |
*/ |
|
597 |
function _convert_blockquotes(&$text) |
|
598 |
{ |
|
599 |
if (preg_match_all('/<\/*blockquote[^>]*>/i', $text, $matches, PREG_OFFSET_CAPTURE)) { |
|
600 |
$level = 0; |
|
601 |
$diff = 0; |
|
602 |
foreach ($matches[0] as $m) { |
|
603 |
if ($m[0][0] == '<' && $m[0][1] == '/') { |
|
604 |
$level--; |
|
605 |
if ($level < 0) { |
|
606 |
$level = 0; // malformed HTML: go to next blockquote |
|
607 |
} |
|
608 |
else if ($level > 0) { |
|
609 |
// skip inner blockquote |
|
610 |
} |
|
611 |
else { |
|
612 |
$end = $m[1]; |
|
613 |
$len = $end - $taglen - $start; |
|
614 |
// Get blockquote content |
|
615 |
$body = substr($text, $start + $taglen - $diff, $len); |
|
616 |
|
|
617 |
// Set text width |
|
618 |
$p_width = $this->width; |
|
619 |
if ($this->width > 0) $this->width -= 2; |
|
620 |
// Convert blockquote content |
|
621 |
$body = trim($body); |
|
622 |
$this->_converter($body); |
|
623 |
// Add citation markers and create PRE block |
|
624 |
$body = preg_replace('/((^|\n)>*)/', '\\1> ', trim($body)); |
|
625 |
$body = '<pre>' . htmlspecialchars($body) . '</pre>'; |
|
626 |
// Re-set text width |
|
627 |
$this->width = $p_width; |
|
628 |
// Replace content |
|
629 |
$text = substr($text, 0, $start - $diff) |
|
630 |
. $body . substr($text, $end + strlen($m[0]) - $diff); |
|
631 |
|
|
632 |
$diff = $len + $taglen + strlen($m[0]) - strlen($body); |
|
633 |
unset($body); |
|
634 |
} |
|
635 |
} |
|
636 |
else { |
|
637 |
if ($level == 0) { |
|
638 |
$start = $m[1]; |
|
639 |
$taglen = strlen($m[0]); |
|
640 |
} |
|
641 |
$level ++; |
|
642 |
} |
|
643 |
} |
6972cc
|
644 |
} |
8ac6fd
|
645 |
} |
f50cc7
|
646 |
|
A |
647 |
/** |
|
648 |
* Callback function for preg_replace_callback use. |
|
649 |
* |
|
650 |
* @param array PREG matches |
|
651 |
* @return string |
|
652 |
*/ |
d483cd
|
653 |
private function _preg_callback($matches) |
f50cc7
|
654 |
{ |
6972cc
|
655 |
switch($matches[1]) { |
T |
656 |
case 'b': |
|
657 |
case 'strong': |
|
658 |
return $this->_strtoupper($matches[2]); |
da1722
|
659 |
case 'th': |
6972cc
|
660 |
return $this->_strtoupper("\t\t". $matches[2] ."\n"); |
T |
661 |
case 'h': |
|
662 |
return $this->_strtoupper("\n\n". $matches[2] ."\n\n"); |
|
663 |
case 'a': |
29c542
|
664 |
// Remove spaces in URL (#1487805) |
A |
665 |
$url = str_replace(' ', '', $matches[3]); |
|
666 |
return $this->_build_link_list($url, $matches[4]); |
6972cc
|
667 |
} |
f50cc7
|
668 |
} |
29c542
|
669 |
|
f50cc7
|
670 |
/** |
d483cd
|
671 |
* Callback function for preg_replace_callback use in PRE content handler. |
A |
672 |
* |
|
673 |
* @param array PREG matches |
|
674 |
* @return string |
|
675 |
*/ |
|
676 |
private function _preg_pre_callback($matches) |
|
677 |
{ |
|
678 |
return $this->pre_content; |
|
679 |
} |
|
680 |
|
|
681 |
/** |
f50cc7
|
682 |
* Strtoupper multibyte wrapper function |
A |
683 |
* |
|
684 |
* @param string |
|
685 |
* @return string |
|
686 |
*/ |
d483cd
|
687 |
private function _strtoupper($str) |
f50cc7
|
688 |
{ |
6972cc
|
689 |
if (function_exists('mb_strtoupper')) |
T |
690 |
return mb_strtoupper($str); |
|
691 |
else |
|
692 |
return strtoupper($str); |
f50cc7
|
693 |
} |
4e17e6
|
694 |
} |