| [ Index ] |
|
Code source de Plume CMS 1.2.2 |
1 <?php 2 3 /************************************************************************* 4 * * 5 * class.html2text.inc * 6 * * 7 ************************************************************************* 8 * * 9 * Converts HTML to formatted plain text * 10 * * 11 * Copyright (c) 2003 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: 11/07/03 * 30 * * 31 *************************************************************************/ 32 33 34 /** 35 * Takes HTML and converts it to formatted, plain text. 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 * @author Jon Abernathy <jon@chuggnutt.com> 47 * @version 0.4 48 * @since PHP 4.0.2 49 */ 50 class html2text 51 { 52 53 /** 54 * Contains the HTML content to convert. 55 * 56 * @var string $html 57 * @access public 58 */ 59 var $html; 60 61 /** 62 * Contains the converted, formatted text. 63 * 64 * @var string $text 65 * @access public 66 */ 67 var $text; 68 69 /** 70 * Maximum width of the formatted text, in columns. 71 * 72 * @var integer $width 73 * @access public 74 */ 75 var $width = 70; 76 77 /** 78 * List of preg* regular expression patterns to search for, 79 * used in conjunction with $replace. 80 * 81 * @var array $search 82 * @access public 83 * @see $replace 84 */ 85 var $search = array( 86 "/\r/", // Non-legal carriage return 87 "/[\n\t]+/", // Newlines and tabs 88 '/<script[^>]*>.*?<\/script>/i', // <script>s -- which strip_tags supposedly has problems with 89 //'/<!-- .* -->/', // Comments -- which strip_tags might have problem a with 90 '/<h[123][^>]*>(.+?)<\/h[123]>/ie', // H1 - H3 91 '/<h[456][^>]*>(.+?)<\/h[456]>/ie', // H4 - H6 92 '/<p[^>]*>/i', // <P> 93 '/<br[^>]*>/i', // <br> 94 '/<b[^>]*>(.+?)<\/b>/ie', // <b> 95 '/<i[^>]*>(.+?)<\/i>/i', // <i> 96 '/(<ul[^>]*>|<\/ul>)/i', // <ul> and </ul> 97 '/<li[^>]*>/i', // <li> 98 '/<\/li[^>]*>/i', // </li> 99 '/<a href="([^"]+)"[^>]*>(.+?)<\/a>/ie', // <a href=""> 100 '/<hr[^>]*>/i', // <hr> 101 '/(<table[^>]*>|<\/table>)/i', // <table> and </table> 102 '/(<tr[^>]*>|<\/tr>)/i', // <tr> and </tr> 103 '/<td[^>]*>(.+?)<\/td>/i', // <td> and </td> 104 '/ /i', 105 '/"/i', 106 '/>/i', 107 '/</i', 108 '/&/i', 109 '/©/i', 110 '/™/i', 111 '/“/', 112 '/”/', 113 '/–/', 114 '/’/', 115 '/&/', 116 '/©/', 117 '/™/', 118 '/—/', 119 '/“/', 120 '/”/', 121 '/•/', 122 '/®/i' 123 ); 124 125 /** 126 * List of pattern replacements corresponding to patterns searched. 127 * 128 * @var array $replace 129 * @access public 130 * @see $search 131 */ 132 var $replace = array( 133 '', // Non-legal carriage return 134 ' ', // Newlines and tabs 135 '', // <script>s -- which strip_tags supposedly has problems with 136 //'', // Comments -- which strip_tags might have problem a with 137 "strtoupper(\"\n\n\\1\n\n\")", // H1 - H3 138 "ucwords(\"\n\n\\1\n\n\")", // H4 - H6 139 "\n\n\t", // <P> 140 "\n", // <br> 141 'strtoupper("\\1")', // <b> 142 '_\\1_', // <i> 143 "\n\n", // <ul> and </ul> 144 "\t*", // <li> 145 "\n", // </li> 146 '$this->_build_link_list($link_count++, "\\1", "\\2")', 147 // <a href=""> 148 "\n-------------------------\n", // <hr> 149 "\n\n", // <table> and </table> 150 "\n", // <tr> and </tr> 151 "\t\t\\1\n", // <td> and </td> 152 ' ', 153 '"', 154 '>', 155 '<', 156 '&', 157 '(c)', 158 '(tm)', 159 '"', 160 '"', 161 '-', 162 "'", 163 '&', 164 '(c)', 165 '(tm)', 166 '--', 167 '"', 168 '"', 169 '*', 170 '(R)' 171 ); 172 173 /** 174 * Indicates whether content in the $html variable has been converted yet. 175 * 176 * @var boolean $converted 177 * @access private 178 * @see $html, $text 179 */ 180 var $_converted = false; 181 182 /** 183 * Contains URL addresses from links to be rendered in plain text. 184 * 185 * @var string $link_list 186 * @access private 187 * @see _build_link_list() 188 */ 189 var $_link_list; 190 191 /** 192 * Constructor. 193 * 194 * If the HTML source string (or file) is supplied, the class 195 * will instantiate with that source propagated, all that has 196 * to be done it to call get_text(). 197 * 198 * @param string $source HTML content 199 * @param boolean $from_file Indicates $source is a file to pull content from 200 * @access public 201 * @return void 202 */ 203 function html2text( $source = '', $from_file = false ) 204 { 205 if ( !empty($source) ) { 206 $this->set_html($source, $from_file); 207 } 208 } 209 210 /** 211 * Loads source HTML into memory, either from $source string or a file. 212 * 213 * @param string $source HTML content 214 * @param boolean $from_file Indicates $source is a file to pull content from 215 * @access public 216 * @return void 217 */ 218 function set_html( $source, $from_file = false ) 219 { 220 $this->html = $source; 221 222 if ( $from_file && file_exists($source) ) { 223 $fp = fopen($source, 'r'); 224 $this->html = fread($fp, filesize($source)); 225 fclose($fp); 226 } 227 228 $this->_converted = false; 229 } 230 231 /** 232 * Returns the text, converted from HTML. 233 * 234 * @access public 235 * @return string 236 */ 237 function get_text() 238 { 239 if ( !$this->_converted ) { 240 $this->_convert(); 241 } 242 243 return $this->text; 244 } 245 246 /** 247 * Prints the text, converted from HTML. 248 * 249 * @access public 250 * @return void 251 */ 252 function print_text() 253 { 254 print $this->get_text(); 255 } 256 257 /** 258 * Alias to print_text(), operates identically. 259 * 260 * @access public 261 * @return void 262 * @see print_text() 263 */ 264 function p() 265 { 266 print $this->get_text(); 267 } 268 269 /** 270 * Workhorse function that does actual conversion. 271 * 272 * First performs custom tag replacement specified by $search and 273 * $replace arrays. Then strips any remaining HTML tags, reduces whitespace 274 * and newlines to a readable format, and word wraps the text to 275 * $width characters. 276 * 277 * @access private 278 * @return void 279 */ 280 function _convert() 281 { 282 // Variables used for building the link list 283 $link_count = 1; 284 $this->_link_list = ''; 285 286 $text = trim(stripslashes($this->html)); 287 288 // Run our defined search-and-replace 289 $text = preg_replace($this->search, $this->replace, trim($text)); 290 291 // Strip any other HTML tags 292 $text = strip_tags($text); 293 294 // Bring down number of empty lines to 2 max 295 $text = preg_replace("/\n[[:space:]]+\n/", "\n\n", $text); 296 $text = preg_replace("/[\n]{3,}/", "\n\n", $text); 297 298 // Add link list 299 if ( !empty($this->_link_list) ) { 300 $text .= "\n\n".__('Links:')."\n------\n" . $this->_link_list; 301 } 302 303 // Wrap the text to a readable format 304 // for PHP versions >= 4.0.2. Default width is 75 305 $text = wordwrap($text, $this->width); 306 307 $this->text = $text; 308 309 $this->_converted = true; 310 } 311 312 /** 313 * Helper function called by preg_replace() on link replacement. 314 * 315 * Maintains an internal list of links to be displayed at the end of the 316 * text, with numeric indices to the original point in the text they 317 * appeared. 318 * 319 * @param integer $link_count Counter tracking current link number 320 * @param string $link URL of the link 321 * @param string $display Part of the text to associate number with 322 * @access private 323 * @return string 324 */ 325 function _build_link_list($link_count, $link, $display) 326 { 327 $this->_link_list .= "[$link_count] $link\n"; 328 329 return $display . '[' . $link_count . ']'; 330 } 331 332 } 333 334 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Mon Nov 26 11:57:01 2007 | par Balluche grâce à PHPXref 0.7 |
|