| [ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 3 require_once 'Horde/String.php'; 4 5 /** 6 * Takes HTML and converts it to formatted, plain text. 7 * 8 * Parameters: 9 * <pre> 10 * charset -- The charset to use for html_entity_decode() calls. 11 * width -- The wrapping width. 12 * wrap -- Whether to wrap the text or not. 13 * </pre> 14 * 15 * $Horde: framework/Text_Filter/Filter/html2text.php,v 1.4.10.12 2006/01/01 21:28:38 jan Exp $ 16 * 17 * Copyright 2003-2006 Jon Abernathy <jon@chuggnutt.com> 18 * Original source: http://www.chuggnutt.com/html2text.php 19 * Copyright 2004-2006 Jan Schneider <jan@horde.org> 20 * 21 * See the enclosed file COPYING for license information (LGPL). If you did 22 * not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 23 * 24 * @author Jon Abernathy <jon@chuggnutt.com> 25 * @author Jan Schneider <jan@horde.org> 26 * @since Horde 3.0 27 * @package Horde_Text 28 */ 29 class Text_Filter_html2text extends Text_Filter { 30 31 /** 32 * Filter parameters. 33 * 34 * @var array 35 */ 36 var $_params = array('charset' => null, 37 'width' => 70, 38 'wrap' => true); 39 40 /** 41 * Executes any code necessaray before applying the filter patterns. 42 * 43 * @param string $text The text before the filtering. 44 * 45 * @return string The modified text. 46 */ 47 function preProcess($text) 48 { 49 global $_html2text_state; 50 51 if (is_null($this->_params['charset'])) { 52 $this->_params['charset'] = isset($GLOBALS['_HORDE_STRING_CHARSET']) ? $GLOBALS['_HORDE_STRING_CHARSET'] : 'ISO-8859-1'; 53 } 54 55 $_html2text_state['linkList'] = ''; 56 $_html2text_state['linkCount'] = 0; 57 58 return trim($text); 59 } 60 61 /** 62 * Returns a hash with replace patterns. 63 * 64 * @return array Patterns hash. 65 */ 66 function getPatterns() 67 { 68 $regexp = array( 69 // Non-legal carriage return. 70 '/\r/' => '', 71 72 // Leading and trailing whitespace. 73 '/^\s*(.*?)\s*$/m' => '\1', 74 75 // Normalize <br>. 76 '/<br[^>]*>([^\n])/i' => "<br>\n\\1", 77 78 // Newlines and tabs. 79 '/[\n\t]+/' => ' ', 80 81 // <script>s -- which strip_tags() supposedly has problems with. 82 '/<script[^>]*>.*?<\/script>/i' => '', 83 84 // <style>s -- which strip_tags() supposedly has problems with. 85 '/<style[^>]*>.*?<\/style>/i' => '', 86 87 // Comments -- which strip_tags() might have a problem with. 88 // //'/<!-- .* -->/' => '', 89 90 // h1 - h3 91 '/<h[123][^>]*>(.+?)<\/h[123]> ?/ie' => 'strtoupper("\n\n" . \'\1\' . "\n\n")', 92 93 // h4 - h6 94 '/<h[456][^>]*>(.+?)<\/h[456]> ?/ie' => 'ucwords("\n\n" . \'\1\' . "\n\n")', 95 96 // <p> 97 '/<p[^>]*> ?/i' => "\n\n ", 98 99 // <br> 100 '/<br[^>]*> ?/i' => "\n", 101 102 // <b> 103 '/<b[^>]*>(.+?)<\/b>/ie' => 'strtoupper(\'\1\')', 104 105 // <strong> 106 '/<strong[^>]*>(.+?)<\/strong>/ie' => 'strtoupper(\'\1\')', 107 '/<span\\s+style="font-weight:\\s*bold.*">(.+?)<\/span>/ie' => 'strtoupper(\'\1\')', 108 109 // <i> 110 '/<i[^>]*>(.+?)<\/i>/i' => '/\\1/', 111 112 // <em> 113 '/<em[^>]*>(.+?)<\/em>/i' => '/\\1/', 114 115 // <u> 116 '/<u[^>]*>(.+?)<\/u>/i' => '_\\1_', 117 118 // <ul>/<ol> and </ul>/</ol> 119 '/(<(u|o)l[^>]*>| ?<\/(u|o)l>) ?/i' => "\n\n", 120 121 // <li> 122 '/ ?<li[^>]*>/i' => "\n * ", 123 124 // <a href=""> 125 '/<a href="([^"]+)"[^>]*>(.+?)<\/a>/ie' => 'Text_Filter_html2text::_buildLinkList($GLOBALS["_html2text_state"]["linkCount"], \'\1\', \'\2\')', 126 127 // <hr> 128 '/<hr[^>]*> ?/i' => "\n-------------------------\n", 129 130 // <table> and </table> 131 '/(<table[^>]*>| ?<\/table>) ?/i' => "\n\n", 132 133 // <tr> 134 '/ ?<tr[^>]*> ?/i' => "\n\t", 135 136 // <td> and </td> 137 '/ ?<td[^>]*>(.+?)<\/td> ?/i' => '\1' . "\t\t", 138 '/\t\t<\/tr>/i' => '', 139 140 // entities 141 '/ /i' => ' ', 142 '/™/i' => '(tm)', 143 '/&#(\d+);/e' => 'String::convertCharset(Text_Filter_html2text::_int2utf8(\'\1\'), "UTF-8", "' . $this->_params['charset'] . '")', 144 145 // Some mailers (e.g. Hotmail) use the following div tag as a way 146 // to define a block of text. 147 '/<div class=rte>(.+?)<\/div> ?/i' => '\1' . "\n" 148 ); 149 150 return array('regexp' => $regexp); 151 } 152 153 /** 154 * Executes any code necessaray after applying the filter 155 * patterns. 156 * 157 * @param string $text The text after the filtering. 158 * 159 * @return string The modified text. 160 */ 161 function postProcess($text) 162 { 163 global $_html2text_state; 164 165 /* Strip any other HTML tags. */ 166 $text = strip_tags($text); 167 168 /* Convert html entities. */ 169 $text = @html_entity_decode($text, ENT_QUOTES, $this->_params['charset']); 170 171 /* Bring down number of empty lines to 2 max. */ 172 $text = preg_replace("/\n[[:space:]]+\n/", "\n\n", $text); 173 $text = preg_replace("/[\n]{3,}/", "\n\n", $text); 174 175 /* Wrap the text to a readable format. */ 176 if ($this->_params['wrap']) { 177 $text = wordwrap($text, $this->_params['width']); 178 } 179 180 /* Add link list. */ 181 if (!empty($_html2text_state['linkList'])) { 182 $text .= "\n\n" . _("Links") . ":\n" . str_repeat('-', String::length(_("Links")) + 1) . "\n" . $_html2text_state['linkList']; 183 } 184 185 return $text; 186 } 187 188 /** 189 * Returns the UTF-8 character sequence of a Unicode value. 190 * 191 * @param integer $num A Unicode value. 192 * 193 * @return string The UTF-8 string. 194 */ 195 function _int2utf8($num) 196 { 197 if ($num < 128) { 198 return chr($num); 199 } 200 if ($num < 2048) { 201 return chr(($num >> 6) + 192) . chr(($num & 63) + 128); 202 } 203 if ($num < 65536) { 204 return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . 205 chr(($num & 63) + 128); 206 } 207 if ($num < 2097152) { 208 return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . 209 chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128); 210 } 211 return ''; 212 } 213 214 /** 215 * Helper function called by preg_replace() on link replacement. 216 * 217 * Maintains an internal list of links to be displayed at the end 218 * of the text, with numeric indices to the original point in the 219 * text they appeared. 220 * 221 * @access private 222 * 223 * @param integer $link_count Counter tracking current link number. 224 * @param string $link URL of the link. 225 * @param string $display Part of the text to associate number with. 226 * 227 * @return string The link replacement. 228 */ 229 function _buildLinkList($link_count, $link, $display) 230 { 231 global $_html2text_state; 232 233 $_html2text_state['linkCount']++; 234 $_html2text_state['linkList'] .= '[' . $_html2text_state['linkCount'] . "] $link\n"; 235 return $display . '[' . $_html2text_state['linkCount'] . ']'; 236 } 237 238 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |