minor improvement to tabs style
[openemr.git] / library / classes / html2text.class.php
blob80183d86d061f82a6a6115a069f0d2629221ad79
1 <?php
3 /*************************************************************************
4 * *
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 *************************************************************************/
34 /**
35 * Takes HTML and converts it to formatted, plain text.
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.
40 * Updated set_html() function's file reading mechanism, 9/25/03.
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.
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.
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.
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.
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.
66 * Thanks to Jacob Chandler for pointing out another link condition
67 * for the _build_link_list() function: "https".
68 * Updated 04/06/05.
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.
75 * *** Big housecleaning updates below:
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.
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.
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.
92 * Thanks to Daniel Schledermann (http://www.typoconsult.dk/) for
93 * suggesting a valuable fix with <a> tag handling.
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.
101 * *** End of the housecleaning updates. Updated 08/08/07.
103 * Modified 2015-10-04 by Rod Roark <rod@sunsetsystems.com>
104 * Added an argument to the _convert function to allow omission of the
105 * links at the end.
107 * @author Jon Abernathy <jon@chuggnutt.com>
108 * @version 1.0.0
109 * @since PHP 4.0.2
111 class html2text
115 * Contains the HTML content to convert.
117 * @var string $html
118 * @access public
120 var $html;
123 * Contains the converted, formatted text.
125 * @var string $text
126 * @access public
128 var $text;
131 * Maximum width of the formatted text, in columns.
133 * Set this value to 0 (or less) to ignore word wrapping
134 * and not constrain text to a fixed-width column.
136 * @var integer $width
137 * @access public
139 var $width = 70;
142 * List of preg* regular expression patterns to search for,
143 * used in conjunction with $replace.
145 * @var array $search
146 * @access public
147 * @see $replace
149 var $search = array(
150 "/\r/", // Non-legal carriage return
151 "/[\n\t]+/", // Newlines and tabs
152 '/[ ]{2,}/', // Runs of spaces, pre-handling
153 '/<script[^>]*>.*?<\/script>/i', // <script>s -- which strip_tags supposedly has problems with
154 '/<style[^>]*>.*?<\/style>/i', // <style>s -- which strip_tags supposedly has problems with
155 //'/<!-- .* -->/', // Comments -- which strip_tags might have problem a with
156 '/<h[123][^>]*>(.*?)<\/h[123]>/ie', // H1 - H3
157 '/<h[456][^>]*>(.*?)<\/h[456]>/ie', // H4 - H6
158 '/<p[^>]*>/i', // <P>
159 '/<br[^>]*>/i', // <br>
160 '/<b[^>]*>(.*?)<\/b>/ie', // <b>
161 '/<strong[^>]*>(.*?)<\/strong>/ie', // <strong>
162 '/<i[^>]*>(.*?)<\/i>/i', // <i>
163 '/<em[^>]*>(.*?)<\/em>/i', // <em>
164 '/(<ul[^>]*>|<\/ul>)/i', // <ul> and </ul>
165 '/(<ol[^>]*>|<\/ol>)/i', // <ol> and </ol>
166 '/<li[^>]*>(.*?)<\/li>/i', // <li> and </li>
167 '/<li[^>]*>/i', // <li>
168 '/<a [^>]*href="([^"]+)"[^>]*>(.*?)<\/a>/ie',
169 // <a href="">
170 '/<hr[^>]*>/i', // <hr>
171 '/(<table[^>]*>|<\/table>)/i', // <table> and </table>
172 '/(<tr[^>]*>|<\/tr>)/i', // <tr> and </tr>
173 '/<td[^>]*>(.*?)<\/td>/i', // <td> and </td>
174 '/<th[^>]*>(.*?)<\/th>/ie', // <th> and </th>
175 '/&(nbsp|#160);/i', // Non-breaking space
176 '/&(quot|rdquo|ldquo|#8220|#8221|#147|#148);/i',
177 // Double quotes
178 '/&(apos|rsquo|lsquo|#8216|#8217);/i', // Single quotes
179 '/&gt;/i', // Greater-than
180 '/&lt;/i', // Less-than
181 '/&(amp|#38);/i', // Ampersand
182 '/&(copy|#169);/i', // Copyright
183 '/&(trade|#8482|#153);/i', // Trademark
184 '/&(reg|#174);/i', // Registered
185 '/&(mdash|#151|#8212);/i', // mdash
186 '/&(ndash|minus|#8211|#8722);/i', // ndash
187 '/&(bull|#149|#8226);/i', // Bullet
188 '/&(pound|#163);/i', // Pound sign
189 '/&(euro|#8364);/i', // Euro sign
190 '/&[^&;]+;/i', // Unknown/unhandled entities
191 '/[ ]{2,}/' // Runs of spaces, post-handling
195 * List of pattern replacements corresponding to patterns searched.
197 * @var array $replace
198 * @access public
199 * @see $search
201 var $replace = array(
202 '', // Non-legal carriage return
203 ' ', // Newlines and tabs
204 ' ', // Runs of spaces, pre-handling
205 '', // <script>s -- which strip_tags supposedly has problems with
206 '', // <style>s -- which strip_tags supposedly has problems with
207 //'', // Comments -- which strip_tags might have problem a with
208 "strtoupper(\"\n\n\\1\n\n\")", // H1 - H3
209 "ucwords(\"\n\n\\1\n\n\")", // H4 - H6
210 "\n\n\t", // <P>
211 "\n", // <br>
212 'strtoupper("\\1")', // <b>
213 'strtoupper("\\1")', // <strong>
214 '_\\1_', // <i>
215 '_\\1_', // <em>
216 "\n\n", // <ul> and </ul>
217 "\n\n", // <ol> and </ol>
218 "\t* \\1\n", // <li> and </li>
219 "\n\t* ", // <li>
220 '$this->_build_link_list("\\1", "\\2")',
221 // <a href="">
222 "\n-------------------------\n", // <hr>
223 "\n\n", // <table> and </table>
224 "\n", // <tr> and </tr>
225 "\t\t\\1\n", // <td> and </td>
226 "strtoupper(\"\t\t\\1\n\")", // <th> and </th>
227 ' ', // Non-breaking space
228 '"', // Double quotes
229 "'", // Single quotes
230 '>',
231 '<',
232 '&',
233 '(c)',
234 '(tm)',
235 '(R)',
236 '--',
237 '-',
238 '*',
239 '£',
240 'EUR', // Euro sign. € ?
241 '', // Unknown/unhandled entities
242 ' ' // Runs of spaces, post-handling
246 * Contains a list of HTML tags to allow in the resulting text.
248 * @var string $allowed_tags
249 * @access public
250 * @see set_allowed_tags()
252 var $allowed_tags = '';
255 * Contains the base URL that relative links should resolve to.
257 * @var string $url
258 * @access public
260 var $url;
263 * Indicates whether content in the $html variable has been converted yet.
265 * @var boolean $_converted
266 * @access private
267 * @see $html, $text
269 var $_converted = false;
272 * Contains URL addresses from links to be rendered in plain text.
274 * @var string $_link_list
275 * @access private
276 * @see _build_link_list()
278 var $_link_list = '';
281 * Number of valid links detected in the text, used for plain text
282 * display (rendered similar to footnotes).
284 * @var integer $_link_count
285 * @access private
286 * @see _build_link_list()
288 var $_link_count = 0;
291 * Constructor.
293 * If the HTML source string (or file) is supplied, the class
294 * will instantiate with that source propagated, all that has
295 * to be done it to call get_text().
297 * @param string $source HTML content
298 * @param boolean $from_file Indicates $source is a file to pull content from
299 * @access public
300 * @return void
302 function __construct( $source = '', $from_file = false )
304 if ( !empty($source) ) {
305 $this->set_html($source, $from_file);
307 $this->set_base_url();
311 * Loads source HTML into memory, either from $source string or a file.
313 * @param string $source HTML content
314 * @param boolean $from_file Indicates $source is a file to pull content from
315 * @access public
316 * @return void
318 function set_html( $source, $from_file = false )
320 $this->html = $source;
322 if ( $from_file && file_exists($source) ) {
323 $fp = fopen($source, 'r');
324 $this->html = fread($fp, filesize($source));
325 fclose($fp);
328 $this->_converted = false;
332 * Returns the text, converted from HTML.
334 * @access public
335 * @return string
337 function get_text()
339 if ( !$this->_converted ) {
340 $this->_convert();
343 return $this->text;
347 * Prints the text, converted from HTML.
349 * @access public
350 * @return void
352 function print_text()
354 print $this->get_text();
358 * Alias to print_text(), operates identically.
360 * @access public
361 * @return void
362 * @see print_text()
364 function p()
366 print $this->get_text();
370 * Sets the allowed HTML tags to pass through to the resulting text.
372 * Tags should be in the form "<p>", with no corresponding closing tag.
374 * @access public
375 * @return void
377 function set_allowed_tags( $allowed_tags = '' )
379 if ( !empty($allowed_tags) ) {
380 $this->allowed_tags = $allowed_tags;
385 * Sets a base URL to handle relative links.
387 * @access public
388 * @return void
390 function set_base_url( $url = '' )
392 if ( empty($url) ) {
393 if ( !empty($_SERVER['HTTP_HOST']) ) {
394 $this->url = 'http://' . $_SERVER['HTTP_HOST'];
395 } else {
396 $this->url = '';
398 } else {
399 // Strip any trailing slashes for consistency (relative
400 // URLs may already start with a slash like "/file.html")
401 if ( substr($url, -1) == '/' ) {
402 $url = substr($url, 0, -1);
404 $this->url = $url;
409 * Workhorse function that does actual conversion.
411 * First performs custom tag replacement specified by $search and
412 * $replace arrays. Then strips any remaining HTML tags, reduces whitespace
413 * and newlines to a readable format, and word wraps the text to
414 * $width characters.
416 * @access private
417 * @return void
419 function _convert($ll=true)
421 // Variables used for building the link list
422 $this->_link_count = 0;
423 $this->_link_list = '';
425 $text = trim(stripslashes($this->html));
427 // Run our defined search-and-replace
428 $text = preg_replace($this->search, $this->replace, $text);
430 // Strip any other HTML tags
431 $text = strip_tags($text, $this->allowed_tags);
433 // Bring down number of empty lines to 2 max
434 $text = preg_replace("/\n\s+\n/", "\n\n", $text);
435 $text = preg_replace("/[\n]{3,}/", "\n\n", $text);
437 // Add link list
438 if ( $ll && !empty($this->_link_list) ) {
439 $text .= "\n\nLinks:\n------\n" . $this->_link_list;
442 // Wrap the text to a readable format
443 // for PHP versions >= 4.0.2. Default width is 75
444 // If width is 0 or less, don't wrap the text.
445 if ( $this->width > 0 ) {
446 $text = wordwrap($text, $this->width);
449 $this->text = $text;
451 $this->_converted = true;
455 * Helper function called by preg_replace() on link replacement.
457 * Maintains an internal list of links to be displayed at the end of the
458 * text, with numeric indices to the original point in the text they
459 * appeared. Also makes an effort at identifying and handling absolute
460 * and relative links.
462 * @param string $link URL of the link
463 * @param string $display Part of the text to associate number with
464 * @access private
465 * @return string
467 function _build_link_list( $link, $display )
469 if ( substr($link, 0, 7) == 'http://' || substr($link, 0, 8) == 'https://' ||
470 substr($link, 0, 7) == 'mailto:' ) {
471 $this->_link_count++;
472 $this->_link_list .= "[" . $this->_link_count . "] $link\n";
473 $additional = ' [' . $this->_link_count . ']';
474 } elseif ( substr($link, 0, 11) == 'javascript:' ) {
475 // Don't count the link; ignore it
476 $additional = '';
477 // what about href="#anchor" ?
478 } else {
479 $this->_link_count++;
480 $this->_link_list .= "[" . $this->_link_count . "] " . $this->url;
481 if ( substr($link, 0, 1) != '/' ) {
482 $this->_link_list .= '/';
484 $this->_link_list .= "$link\n";
485 $additional = ' [' . $this->_link_count . ']';
488 return $display . $additional;