svncommit
2006-09-23 6649b1f0a5db6160d197a13ca79cfd67fbb02d77
commit | author | age
4e17e6 1 <?php
T 2
3 /*************************************************************************
4 *                                                                       *
5 * class.html2text.inc                                                   *
6 *                                                                       *
7 *************************************************************************
8 *                                                                       *
9 * Converts HTML to formatted plain text                                 *
10 *                                                                       *
11 * Copyright (c) 2005 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: 04/06/05                                               *
30 * Modified: 2004/05/19 (tbr)                                            *
31 *                                                                       *
32 *************************************************************************/
33
34
35 /**
36 *  Takes HTML and converts it to formatted, plain text.
37 *
38 *  Thanks to Alexander Krug (http://www.krugar.de/) to pointing out and
39 *  correcting an error in the regexp search array. Fixed 7/30/03.
40 *
41 *  Updated set_html() function's file reading mechanism, 9/25/03.
42 *
43 *  Thanks to Joss Sanglier (http://www.dancingbear.co.uk/) for adding
44 *  several more HTML entity codes to the $search and $replace arrays.
45 *  Updated 11/7/03.
46 *
47 *  Thanks to Darius Kasperavicius (http://www.dar.dar.lt/) for
48 *  suggesting the addition of $allowed_tags and its supporting function
49 *  (which I slightly modified). Updated 3/12/04.
50 *
51 *  Thanks to Justin Dearing for pointing out that a replacement for the
52 *  <TH> tag was missing, and suggesting an appropriate fix.
53 *  Updated 8/25/04.
54 *
55 *  Thanks to Mathieu Collas (http://www.myefarm.com/) for finding a
56 *  display/formatting bug in the _build_link_list() function: email
57 *  readers would show the left bracket and number ("[1") as part of the
58 *  rendered email address.
59 *  Updated 12/16/04.
60 *
61 *  Thanks to Wojciech Bajon (http://histeria.pl/) for submitting code
62 *  to handle relative links, which I hadn't considered. I modified his
63 *  code a bit to handle normal HTTP links and MAILTO links. Also for
64 *  suggesting three additional HTML entity codes to search for.
65 *  Updated 03/02/05.
66 *
67 *  Thanks to Jacob Chandler for pointing out another link condition
68 *  for the _build_link_list() function: "https".
69 *  Updated 04/06/05.
70 *
71 *  @author Jon Abernathy <jon@chuggnutt.com>
72 *  @version 0.6.1
73 *  @since PHP 4.0.2
74 */
75 class html2text
76 {
77
78     /**
79      *  Contains the HTML content to convert.
80      *
81      *  @var string $html
82      *  @access public
83      */
84     var $html;
85
86     /**
87      *  Contains the converted, formatted text.
88      *
89      *  @var string $text
90      *  @access public
91      */
92     var $text;
93
94     /**
95      *  Maximum width of the formatted text, in columns.
96      *
97      *  @var integer $width
98      *  @access public
99      */
100     var $width = 70;
101
102     /**
103      *  List of preg* regular expression patterns to search for,
104      *  used in conjunction with $replace.
105      *
106      *  @var array $search
107      *  @access public
108      *  @see $replace
109      */
110     var $search = array(
111         "/\r/",                                  // Non-legal carriage return
112         "/[\n\t]+/",                             // Newlines and tabs
113         '/<script[^>]*>.*?<\/script>/i',         // <script>s -- which strip_tags supposedly has problems with
114         //'/<!-- .* -->/',                         // Comments -- which strip_tags might have problem a with
115         '/<a href="([^"]+)"[^>]*>(.+?)<\/a>/ie', // <a href="">
116         '/<h[123][^>]*>(.+?)<\/h[123]>/ie',      // H1 - H3
117         '/<h[456][^>]*>(.+?)<\/h[456]>/ie',      // H4 - H6
118         '/<p[^>]*>/i',                           // <P>
119         '/<br[^>]*>/i',                          // <br>
120         '/<b[^>]*>(.+?)<\/b>/ie',                // <b>
121         '/<i[^>]*>(.+?)<\/i>/i',                 // <i>
122         '/(<ul[^>]*>|<\/ul>)/i',                 // <ul> and </ul>
123         '/(<ol[^>]*>|<\/ol>)/i',                 // <ol> and </ol>
124         '/<li[^>]*>/i',                          // <li>
125         '/<hr[^>]*>/i',                          // <hr>
126         '/(<table[^>]*>|<\/table>)/i',           // <table> and </table>
127         '/(<tr[^>]*>|<\/tr>)/i',                 // <tr> and </tr>
128         '/<td[^>]*>(.+?)<\/td>/i',               // <td> and </td>
40ed9b 129         '/<th[^>]*>(.+?)<\/th>/ie',              // <th> and </th>
4e17e6 130         '/&nbsp;/i',
T 131         '/&quot;/i',
132         '/&gt;/i',
133         '/&lt;/i',
40ed9b 134         '/&(amp|#38);/i',
4e17e6 135         '/&copy;/i',
T 136         '/&trade;/i',
137         '/&#8220;/',
138         '/&#8221;/',
139         '/&#8211;/',
40ed9b 140         '/&#(8217|39);/',
4e17e6 141         '/&#169;/',
T 142         '/&#8482;/',
143         '/&#151;/',
144         '/&#147;/',
145         '/&#148;/',
146         '/&#149;/',
147         '/&reg;/i',
148         '/&bull;/i',
149         '/&[&;]+;/i'
150     );
151
152     /**
153      *  List of pattern replacements corresponding to patterns searched.
154      *
155      *  @var array $replace
156      *  @access public
157      *  @see $search
158      */
159     var $replace = array(
160         '',                                     // Non-legal carriage return
161         ' ',                                    // Newlines and tabs
162         '',                                     // <script>s -- which strip_tags supposedly has problems with
163         //'',                                     // Comments -- which strip_tags might have problem a with
164         '$this->_build_link_list("\\1", "\\2")', // <a href="">
165         "strtoupper(\"\n\n\\1\n\n\")",          // H1 - H3
166         "ucwords(\"\n\n\\1\n\n\")",             // H4 - H6
167         "\n\n\t",                               // <P>
168         "\n",                                   // <br>
169         'strtoupper("\\1")',                    // <b>
170         '_\\1_',                                // <i>
171         "\n\n",                                 // <ul> and </ul>
172         "\n\n",                                 // <ol> and </ol>
173         "\t*",                                  // <li>
174         "\n-------------------------\n",        // <hr>
175         "\n\n",                                 // <table> and </table>
176         "\n",                                   // <tr> and </tr>
177         "\t\t\\1\n",                            // <td> and </td>
178         "strtoupper(\"\t\t\\1\n\")",            // <th> and </th>
179         ' ',
180         '"',
181         '>',
182         '<',
183         '&',
184         '(c)',
185         '(tm)',
186         '"',
187         '"',
188         '-',
189         "'",
190         '(c)',
191         '(tm)',
192         '--',
193         '"',
194         '"',
195         '*',
196         '(R)',
197         '*',
198         ''
199     );
200
201     /**
202      *  Contains a list of HTML tags to allow in the resulting text.
203      *
204      *  @var string $allowed_tags
205      *  @access public
206      *  @see set_allowed_tags()
207      */
208     var $allowed_tags = '';
209
210     /**
211      *  Contains the base URL that relative links should resolve to.
212      *
213      *  @var string $url
214      *  @access public
215      */
216     var $url;
217
218     /**
219      *  Indicates whether content in the $html variable has been converted yet.
220      *
221      *  @var boolean $converted
222      *  @access private
223      *  @see $html, $text
224      */
225     var $_converted = false;
226
227     /**
228      *  Contains URL addresses from links to be rendered in plain text.
229      *
230      *  @var string $link_list
231      *  @access private
232      *  @see _build_link_list()
233      */
234     var $_link_list = array();
235
236     /**
237      *  Constructor.
238      *
239      *  If the HTML source string (or file) is supplied, the class
240      *  will instantiate with that source propagated, all that has
241      *  to be done it to call get_text().
242      *
243      *  @param string $source HTML content
244      *  @param boolean $from_file Indicates $source is a file to pull content from
245      *  @access public
246      *  @return void
247      */
248     function html2text( $source = '', $from_file = false )
249     {
250         if ( !empty($source) ) {
251             $this->set_html($source, $from_file);
252         }
253         $this->set_base_url();
254     }
255
256     /**
257      *  Loads source HTML into memory, either from $source string or a file.
258      *
259      *  @param string $source HTML content
260      *  @param boolean $from_file Indicates $source is a file to pull content from
261      *  @access public
262      *  @return void
263      */
264     function set_html( $source, $from_file = false )
265     {
266         $this->html = $source;
267
268         if ( $from_file && file_exists($source) ) {
269             $fp = fopen($source, 'r');
270             $this->html = fread($fp, filesize($source));
271             fclose($fp);
272         }
273
274         $this->_converted = false;
275     }
276
277     /**
278      *  Returns the text, converted from HTML.
279      *
280      *  @access public
281      *  @return string
282      */
283     function get_text()
284     {
285         if ( !$this->_converted ) {
286             $this->_convert();
287         }
288
289         return $this->text;
290     }
291
292     /**
293      *  Prints the text, converted from HTML.
294      *
295      *  @access public
296      *  @return void
297      */
298     function print_text()
299     {
300         print $this->get_text();
301     }
302
303     /**
304      *  Alias to print_text(), operates identically.
305      *
306      *  @access public
307      *  @return void
308      *  @see print_text()
309      */
310     function p()
311     {
312         print $this->get_text();
313     }
314
315     /**
316      *  Sets the allowed HTML tags to pass through to the resulting text.
317      *
318      *  Tags should be in the form "<p>", with no corresponding closing tag.
319      *
320      *  @access public
321      *  @return void
322      */
323     function set_allowed_tags( $allowed_tags = '' )
324     {
325         if ( !empty($allowed_tags) ) {
326             $this->allowed_tags = $allowed_tags;
327         }
328     }
329
330     /**
331      *  Sets a base URL to handle relative links.
332      *
333      *  @access public
334      *  @return void
335      */
336     function set_base_url( $url = '' )
337     {
338         if ( empty($url) ) {
339             $this->url = 'http://' . $_SERVER['HTTP_HOST'];
340         } else {
341             // Strip any trailing slashes for consistency (relative
342             // URLs may already start with a slash like "/file.html")
343             if ( substr($url, -1) == '/' ) {
344                 $url = substr($url, 0, -1);
345             }
346             $this->url = $url;
347         }
348     }
349
350     /**
351      *  Workhorse function that does actual conversion.
352      *
353      *  First performs custom tag replacement specified by $search and
354      *  $replace arrays. Then strips any remaining HTML tags, reduces whitespace
355      *  and newlines to a readable format, and word wraps the text to
356      *  $width characters.
357      *
358      *  @access private
359      *  @return void
360      */
361     function _convert()
362     {
363         // Variables used for building the link list
364         //$link_count = 1;
365         //$this->_link_list = '';
366
367         $text = trim(stripslashes($this->html));
368
369         // Run our defined search-and-replace
370         $text = preg_replace($this->search, $this->replace, $text);
371
372         // Strip any other HTML tags
373         $text = strip_tags($text, $this->allowed_tags);
374
375         // Bring down number of empty lines to 2 max
376         $text = preg_replace("/\n\s+\n/", "\n", $text);
377         $text = preg_replace("/[\n]{3,}/", "\n\n", $text);
378
379         // Add link list
380         if ( sizeof($this->_link_list) ) {
381             $text .= "\n\nLinks:\n------\n";
382             foreach ($this->_link_list as $id => $link) {
383                 $text .= '[' . ($id+1) . '] ' . $link . "\n";
384             }
385         }
386
387         // Wrap the text to a readable format
388         // for PHP versions >= 4.0.2. Default width is 75
389         $text = wordwrap($text, $this->width);
390
391         $this->text = $text;
392
393         $this->_converted = true;
394     }
395
396     /**
397      *  Helper function called by preg_replace() on link replacement.
398      *
399      *  Maintains an internal list of links to be displayed at the end of the
400      *  text, with numeric indices to the original point in the text they
401      *  appeared. Also makes an effort at identifying and handling absolute
402      *  and relative links.
403      *
404      *  @param integer $link_count Counter tracking current link number
405      *  @param string $link URL of the link
406      *  @param string $display Part of the text to associate number with
407      *  @access private
408      *  @return string
409     */
410     function _build_link_list($link, $display)
411       {
412       $link_lc = strtolower($link);
413       
414       if (substr($link_lc, 0, 7) == 'http://' || substr($link_lc, 0, 8) == 'https://' || substr($link_lc, 0, 7) == 'mailto:')
415         {
416         $url = $link;
417         }
418       else
419         {
420         $url = $this->url;
421         if ($link{0} != '/') {
422              $url .= '/';
423             }
424         $url .= $link;
425         }
426
427       $index = array_search($url, $this->_link_list);
428       if ($index===FALSE)
429         {
430         $index = sizeof($this->_link_list);
431         $this->_link_list[$index] = $url;
432         }
433               
434       return $display . ' [' . ($index+1) . ']';
435       }
436 }
437
438 ?>