thomascube
2012-04-17 6ab9e8a6c8474063ea70c727bb3fe107f1a864dc
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
148         '/<script[^>]*>.*?<\/script>/i',         // <script>s -- which strip_tags supposedly has problems with
8ac6fd 149         '/<style[^>]*>.*?<\/style>/i',           // <style>s -- which strip_tags supposedly has problems with
4e17e6 150         '/<p[^>]*>/i',                           // <P>
T 151         '/<br[^>]*>/i',                          // <br>
8ac6fd 152         '/<i[^>]*>(.*?)<\/i>/i',                 // <i>
A 153         '/<em[^>]*>(.*?)<\/em>/i',               // <em>
4e17e6 154         '/(<ul[^>]*>|<\/ul>)/i',                 // <ul> and </ul>
T 155         '/(<ol[^>]*>|<\/ol>)/i',                 // <ol> and </ol>
8ac6fd 156         '/<li[^>]*>(.*?)<\/li>/i',               // <li> and </li>
4e17e6 157         '/<li[^>]*>/i',                          // <li>
T 158         '/<hr[^>]*>/i',                          // <hr>
f6b282 159         '/<div[^>]*>/i',                         // <div>
4e17e6 160         '/(<table[^>]*>|<\/table>)/i',           // <table> and </table>
T 161         '/(<tr[^>]*>|<\/tr>)/i',                 // <tr> and </tr>
8ac6fd 162         '/<td[^>]*>(.*?)<\/td>/i',               // <td> and </td>
4e17e6 163     );
T 164
165     /**
166      *  List of pattern replacements corresponding to patterns searched.
167      *
168      *  @var array $replace
169      *  @access public
170      *  @see $search
171      */
172     var $replace = array(
173         '',                                     // Non-legal carriage return
174         ' ',                                    // Newlines and tabs
175         '',                                     // <script>s -- which strip_tags supposedly has problems with
8ac6fd 176         '',                                     // <style>s -- which strip_tags supposedly has problems with
f6b282 177         "\n\n",                                 // <P>
4e17e6 178         "\n",                                   // <br>
T 179         '_\\1_',                                // <i>
8ac6fd 180         '_\\1_',                                // <em>
4e17e6 181         "\n\n",                                 // <ul> and </ul>
T 182         "\n\n",                                 // <ol> and </ol>
8ac6fd 183         "\t* \\1\n",                            // <li> and </li>
A 184         "\n\t* ",                               // <li>
6972cc 185         "\n-------------------------\n",        // <hr>
11bcac 186         "<div>\n",                              // <div>
6972cc 187         "\n\n",                                 // <table> and </table>
4e17e6 188         "\n",                                   // <tr> and </tr>
T 189         "\t\t\\1\n",                            // <td> and </td>
ca0cd0 190     );
A 191
192     /**
193      *  List of preg* regular expression patterns to search for,
194      *  used in conjunction with $ent_replace.
195      *
196      *  @var array $ent_search
197      *  @access public
198      *  @see $ent_replace
199      */
200     var $ent_search = array(
201         '/&(nbsp|#160);/i',                      // Non-breaking space
202         '/&(quot|rdquo|ldquo|#8220|#8221|#147|#148);/i',
203                                                  // Double quotes
204         '/&(apos|rsquo|lsquo|#8216|#8217);/i',   // Single quotes
205         '/&gt;/i',                               // Greater-than
206         '/&lt;/i',                               // Less-than
207         '/&(copy|#169);/i',                      // Copyright
208         '/&(trade|#8482|#153);/i',               // Trademark
209         '/&(reg|#174);/i',                       // Registered
210         '/&(mdash|#151|#8212);/i',               // mdash
211         '/&(ndash|minus|#8211|#8722);/i',        // ndash
212         '/&(bull|#149|#8226);/i',                // Bullet
213         '/&(pound|#163);/i',                     // Pound sign
214         '/&(euro|#8364);/i',                     // Euro sign
215         '/&(amp|#38);/i',                        // Ampersand: see _converter()
216         '/[ ]{2,}/',                             // Runs of spaces, post-handling
217     );
218
219     /**
220      *  List of pattern replacements corresponding to patterns searched.
221      *
222      *  @var array $ent_replace
223      *  @access public
224      *  @see $ent_search
225      */
226     var $ent_replace = array(
8ac6fd 227         ' ',                                    // Non-breaking space
A 228         '"',                                    // Double quotes
229         "'",                                    // Single quotes
4e17e6 230         '>',
T 231         '<',
232         '(c)',
233         '(tm)',
234         '(R)',
8ac6fd 235         '--',
A 236         '-',
4e17e6 237         '*',
300fc6 238         '£',
8ac6fd 239         'EUR',                                  // Euro sign. Â€ ?
6084d7 240         '|+|amp|+|',                            // Ampersand: see _converter()
ca0cd0 241         ' ',                                    // Runs of spaces, post-handling
f50cc7 242     );
A 243
244     /**
245      *  List of preg* regular expression patterns to search for
246      *  and replace using callback function.
247      *
248      *  @var array $callback_search
249      *  @access public
250      */
251     var $callback_search = array(
f35995 252         '/<(a) [^>]*href=("|\')([^"\']+)\2[^>]*>(.*?)<\/a>/i',
A 253                                                    // <a href="">
f50cc7 254         '/<(h)[123456][^>]*>(.*?)<\/h[123456]>/i', // H1 - H3
A 255         '/<(b)[^>]*>(.*?)<\/b>/i',                 // <b>
256         '/<(strong)[^>]*>(.*?)<\/strong>/i',       // <strong>
257         '/<(th)[^>]*>(.*?)<\/th>/i',               // <th> and </th>
7353fa 258     );
A 259
8ac6fd 260    /**
A 261     *  List of preg* regular expression patterns to search for in PRE body,
262     *  used in conjunction with $pre_replace.
263     *
264     *  @var array $pre_search
265     *  @access public
266     *  @see $pre_replace
267     */
7789fd 268     var $pre_search = array(
6972cc 269         "/\n/",
T 270         "/\t/",
271         '/ /',
272         '/<pre[^>]*>/',
273         '/<\/pre>/'
7353fa 274     );
A 275
276     /**
277      *  List of pattern replacements corresponding to patterns searched for PRE body.
278      *
279      *  @var array $pre_replace
280      *  @access public
281      *  @see $pre_search
282      */
7789fd 283     var $pre_replace = array(
6972cc 284         '<br>',
T 285         '&nbsp;&nbsp;&nbsp;&nbsp;',
286         '&nbsp;',
287         '',
288         ''
4e17e6 289     );
T 290
291     /**
292      *  Contains a list of HTML tags to allow in the resulting text.
293      *
294      *  @var string $allowed_tags
295      *  @access public
296      *  @see set_allowed_tags()
297      */
298     var $allowed_tags = '';
299
300     /**
301      *  Contains the base URL that relative links should resolve to.
302      *
303      *  @var string $url
304      *  @access public
305      */
306     var $url;
307
308     /**
309      *  Indicates whether content in the $html variable has been converted yet.
310      *
8ac6fd 311      *  @var boolean $_converted
4e17e6 312      *  @access private
T 313      *  @see $html, $text
314      */
315     var $_converted = false;
316
317     /**
318      *  Contains URL addresses from links to be rendered in plain text.
319      *
43c40f 320      *  @var array $_link_list
4e17e6 321      *  @access private
T 322      *  @see _build_link_list()
323      */
43c40f 324     var $_link_list = array();
4e17e6 325
ca0cd0 326     /**
A 327      * Boolean flag, true if a table of link URLs should be listed after the text.
328      *
329      * @var boolean $_do_links
330      * @access private
331      * @see html2text()
e7f85b 332      */
A 333     var $_do_links = true;
ca0cd0 334
4e17e6 335     /**
T 336      *  Constructor.
337      *
338      *  If the HTML source string (or file) is supplied, the class
339      *  will instantiate with that source propagated, all that has
340      *  to be done it to call get_text().
341      *
342      *  @param string $source HTML content
343      *  @param boolean $from_file Indicates $source is a file to pull content from
e7f85b 344      *  @param boolean $do_links Indicate whether a table of link URLs is desired
A 345      *  @param integer $width Maximum width of the formatted text, 0 for no limit
4e17e6 346      *  @access public
T 347      *  @return void
348      */
e7f85b 349     function html2text( $source = '', $from_file = false, $do_links = true, $width = 75 )
4e17e6 350     {
T 351         if ( !empty($source) ) {
352             $this->set_html($source, $from_file);
353         }
6972cc 354
4e17e6 355         $this->set_base_url();
6972cc 356         $this->_do_links = $do_links;
T 357         $this->width = $width;
4e17e6 358     }
T 359
360     /**
361      *  Loads source HTML into memory, either from $source string or a file.
362      *
363      *  @param string $source HTML content
364      *  @param boolean $from_file Indicates $source is a file to pull content from
365      *  @access public
366      *  @return void
367      */
368     function set_html( $source, $from_file = false )
369     {
370         if ( $from_file && file_exists($source) ) {
6972cc 371             $this->html = file_get_contents($source); 
4e17e6 372         }
8ac6fd 373         else
6972cc 374             $this->html = $source;
4e17e6 375
T 376         $this->_converted = false;
377     }
378
379     /**
380      *  Returns the text, converted from HTML.
381      *
382      *  @access public
383      *  @return string
384      */
385     function get_text()
386     {
387         if ( !$this->_converted ) {
388             $this->_convert();
389         }
390
391         return $this->text;
392     }
393
394     /**
395      *  Prints the text, converted from HTML.
396      *
397      *  @access public
398      *  @return void
399      */
400     function print_text()
401     {
402         print $this->get_text();
403     }
404
405     /**
406      *  Alias to print_text(), operates identically.
407      *
408      *  @access public
409      *  @return void
410      *  @see print_text()
411      */
412     function p()
413     {
414         print $this->get_text();
415     }
416
417     /**
418      *  Sets the allowed HTML tags to pass through to the resulting text.
419      *
420      *  Tags should be in the form "<p>", with no corresponding closing tag.
421      *
422      *  @access public
423      *  @return void
424      */
425     function set_allowed_tags( $allowed_tags = '' )
426     {
427         if ( !empty($allowed_tags) ) {
428             $this->allowed_tags = $allowed_tags;
429         }
430     }
431
432     /**
433      *  Sets a base URL to handle relative links.
434      *
435      *  @access public
436      *  @return void
437      */
438     function set_base_url( $url = '' )
439     {
440         if ( empty($url) ) {
8ac6fd 441             if ( !empty($_SERVER['HTTP_HOST']) ) {
A 442                 $this->url = 'http://' . $_SERVER['HTTP_HOST'];
443             } else {
444                 $this->url = '';
445             }
4e17e6 446         } else {
T 447             // Strip any trailing slashes for consistency (relative
448             // URLs may already start with a slash like "/file.html")
449             if ( substr($url, -1) == '/' ) {
450                 $url = substr($url, 0, -1);
451             }
452             $this->url = $url;
453         }
454     }
455
456     /**
11bcac 457      *  Workhorse function that does actual conversion (calls _converter() method).
4e17e6 458      *
T 459      *  @access private
460      *  @return void
461      */
462     function _convert()
463     {
464         // Variables used for building the link list
43c40f 465         $this->_link_list = array();
4e17e6 466
T 467         $text = trim(stripslashes($this->html));
11bcac 468
A 469         // Convert HTML to TXT
470         $this->_converter($text);
471
472         // Add link list
43c40f 473         if (!empty($this->_link_list)) {
A 474             $text .= "\n\nLinks:\n------\n";
475             foreach ($this->_link_list as $idx => $url) {
476                 $text .= '[' . ($idx+1) . '] ' . $url . "\n";
477             }
11bcac 478         }
A 479
480         $this->text = $text;
481
482         $this->_converted = true;
483     }
484
485     /**
486      *  Workhorse function that does actual conversion.
487      *
488      *  First performs custom tag replacement specified by $search and
489      *  $replace arrays. Then strips any remaining HTML tags, reduces whitespace
490      *  and newlines to a readable format, and word wraps the text to
491      *  $width characters.
492      *
493      *  @param string Reference to HTML content string
494      *
495      *  @access private
496      *  @return void
497      */
498     function _converter(&$text)
499     {
500         // Convert <BLOCKQUOTE> (before PRE!)
501         $this->_convert_blockquotes($text);
4e17e6 502
6972cc 503         // Convert <PRE>
8ac6fd 504         $this->_convert_pre($text);
300fc6 505
ca0cd0 506         // Run our defined tags search-and-replace
4e17e6 507         $text = preg_replace($this->search, $this->replace, $text);
ca0cd0 508
A 509         // Run our defined tags search-and-replace with callback
510         $text = preg_replace_callback($this->callback_search, array('html2text', '_preg_callback'), $text);
511
512         // Strip any other HTML tags
513         $text = strip_tags($text, $this->allowed_tags);
514
515         // Run our defined entities/characters search-and-replace
516         $text = preg_replace($this->ent_search, $this->ent_replace, $text);
4e17e6 517
6972cc 518         // Replace known html entities
T 519         $text = html_entity_decode($text, ENT_COMPAT, 'UTF-8');
4e0419 520
755900 521         // Remove unknown/unhandled entities (this cannot be done in search-and-replace block)
6084d7 522         $text = preg_replace('/&([a-zA-Z0-9]{2,6}|#[0-9]{2,4});/', '', $text);
A 523
524         // Convert "|+|amp|+|" into "&", need to be done after handling of unknown entities
525         // This properly handles situation of "&amp;quot;" in input string
526         $text = str_replace('|+|amp|+|', '&', $text);
755900 527
4e17e6 528         // Bring down number of empty lines to 2 max
8ac6fd 529         $text = preg_replace("/\n\s+\n/", "\n\n", $text);
4e17e6 530         $text = preg_replace("/[\n]{3,}/", "\n\n", $text);
T 531
4d7fbd 532         // remove leading empty lines (can be produced by eg. P tag on the beginning)
ca0cd0 533         $text = ltrim($text, "\n");
4d7fbd 534
4e17e6 535         // Wrap the text to a readable format
T 536         // for PHP versions >= 4.0.2. Default width is 75
8ac6fd 537         // If width is 0 or less, don't wrap the text.
A 538         if ( $this->width > 0 ) {
539             $text = wordwrap($text, $this->width);
540         }
4e17e6 541     }
T 542
543     /**
544      *  Helper function called by preg_replace() on link replacement.
545      *
546      *  Maintains an internal list of links to be displayed at the end of the
547      *  text, with numeric indices to the original point in the text they
548      *  appeared. Also makes an effort at identifying and handling absolute
549      *  and relative links.
550      *
551      *  @param string $link URL of the link
552      *  @param string $display Part of the text to associate number with
553      *  @access private
554      *  @return string
8ac6fd 555      */
A 556     function _build_link_list( $link, $display )
557     {
43c40f 558         if (!$this->_do_links || empty($link)) {
11bcac 559             return $display;
43c40f 560         }
11bcac 561
43c40f 562         // Ignored link types
A 563         if (preg_match('!^(javascript|mailto|#):!i', $link)) {
564             return $display;
4e17e6 565         }
T 566
43c40f 567         if (preg_match('!^(https?://)!i', $link)) {
A 568             $url = $link;
569         }
570         else {
571             $url = $this->url;
572             if (substr($link, 0, 1) != '/') {
573                 $url .= '/';
574             }
575             $url .= "$link";
576         }
577
578         if (($index = array_search($url, $this->_link_list)) === false) {
579             $this->_link_list[] = $url;
580             $index = count($this->_link_list);
581         }
582
583         return $display . ' [' . ($index+1) . ']';
8ac6fd 584     }
11bcac 585
7353fa 586     /**
A 587      *  Helper function for PRE body conversion.
588      *
589      *  @param string HTML content
590      *  @access private
8ac6fd 591      */
7353fa 592     function _convert_pre(&$text)
8ac6fd 593     {
d483cd 594         // get the content of PRE element
11bcac 595         while (preg_match('/<pre[^>]*>(.*)<\/pre>/ismU', $text, $matches)) {
d483cd 596             // convert the content
A 597             $this->pre_content = sprintf('<div><br>%s<br></div>',
598                 preg_replace($this->pre_search, $this->pre_replace, $matches[1]));
599             // replace the content (use callback because content can contain $0 variable)
600             $text = preg_replace_callback('/<pre[^>]*>.*<\/pre>/ismU', 
601                 array('html2text', '_preg_pre_callback'), $text, 1);
602             // free memory
603             $this->pre_content = '';
11bcac 604         }
A 605     }
606
607     /**
608      *  Helper function for BLOCKQUOTE body conversion.
609      *
610      *  @param string HTML content
611      *  @access private
612      */
613     function _convert_blockquotes(&$text)
614     {
615         if (preg_match_all('/<\/*blockquote[^>]*>/i', $text, $matches, PREG_OFFSET_CAPTURE)) {
616             $level = 0;
617             $diff = 0;
618             foreach ($matches[0] as $m) {
619                 if ($m[0][0] == '<' && $m[0][1] == '/') {
620                     $level--;
621                     if ($level < 0) {
622                         $level = 0; // malformed HTML: go to next blockquote
623                     }
624                     else if ($level > 0) {
625                         // skip inner blockquote
626                     }
627                     else {
628                         $end  = $m[1];
629                         $len  = $end - $taglen - $start;
630                         // Get blockquote content
631                         $body = substr($text, $start + $taglen - $diff, $len);
632
633                         // Set text width
634                         $p_width = $this->width;
635                         if ($this->width > 0) $this->width -= 2;
636                         // Convert blockquote content
637                         $body = trim($body);
638                         $this->_converter($body);
639                         // Add citation markers and create PRE block
640                         $body = preg_replace('/((^|\n)>*)/', '\\1> ', trim($body));
641                         $body = '<pre>' . htmlspecialchars($body) . '</pre>';
642                         // Re-set text width
643                         $this->width = $p_width;
644                         // Replace content
645                         $text = substr($text, 0, $start - $diff)
646                             . $body . substr($text, $end + strlen($m[0]) - $diff);
647
648                         $diff = $len + $taglen + strlen($m[0]) - strlen($body);
649                         unset($body);
650                     }
651                 }
652                 else {
653                     if ($level == 0) {
654                         $start = $m[1];
655                         $taglen = strlen($m[0]);
656                     }
657                     $level ++;
658                 }
659             }
6972cc 660         }
8ac6fd 661     }
f50cc7 662
A 663     /**
664      *  Callback function for preg_replace_callback use.
665      *
666      *  @param  array PREG matches
667      *  @return string
668      */
d483cd 669     private function _preg_callback($matches)
f50cc7 670     {
6972cc 671         switch($matches[1]) {
T 672         case 'b':
673         case 'strong':
f35995 674             return $this->_toupper($matches[2]);
da1722 675         case 'th':
f35995 676             return $this->_toupper("\t\t". $matches[2] ."\n");
6972cc 677         case 'h':
f35995 678             return $this->_toupper("\n\n". $matches[2] ."\n\n");
6972cc 679         case 'a':
29c542 680             // Remove spaces in URL (#1487805)
A 681             $url = str_replace(' ', '', $matches[3]);
682             return $this->_build_link_list($url, $matches[4]);
6972cc 683         }
f50cc7 684     }
29c542 685
f50cc7 686     /**
d483cd 687      *  Callback function for preg_replace_callback use in PRE content handler.
A 688      *
689      *  @param  array PREG matches
690      *  @return string
691      */
692     private function _preg_pre_callback($matches)
693     {
694         return $this->pre_content;
695     }
696
697     /**
f35995 698      * Strtoupper function with HTML tags and entities handling.
f50cc7 699      *
f35995 700      * @param string $str Text to convert
A 701      * @return string Converted text
702      */
703     private function _toupper($str)
704     {
705         // string can containg HTML tags
706         $chunks = preg_split('/(<[^>]*>)/', $str, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
707
708         // convert toupper only the text between HTML tags
709         foreach ($chunks as $idx => $chunk) {
710             if ($chunk[0] != '<') {
711                 $chunks[$idx] = $this->_strtoupper($chunk);
712             }
713         }
714
715         return implode($chunks);
716     }
717
718     /**
719      * Strtoupper multibyte wrapper function with HTML entities handling.
720      *
721      * @param string $str Text to convert
722      * @return string Converted text
f50cc7 723      */
d483cd 724     private function _strtoupper($str)
f50cc7 725     {
67e592 726         $str = html_entity_decode($str, ENT_COMPAT, RCMAIL_CHARSET);
A 727
6972cc 728         if (function_exists('mb_strtoupper'))
67e592 729             $str = mb_strtoupper($str);
6972cc 730         else
67e592 731             $str = strtoupper($str);
A 732
733         $str = htmlspecialchars($str, ENT_COMPAT, RCMAIL_CHARSET);
734
735         return $str;
f50cc7 736     }
4e17e6 737 }