Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
| 9 | * @copyright Copyright (c) 2008, EllisLab, Inc. |
| 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * CodeIgniter Text Helpers |
| 20 | * |
| 21 | * @package CodeIgniter |
| 22 | * @subpackage Helpers |
| 23 | * @category Helpers |
| 24 | * @author ExpressionEngine Dev Team |
| 25 | * @link http://codeigniter.com/user_guide/helpers/text_helper.html |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * Word Limiter |
| 32 | * |
| 33 | * Limits a string to X number of words. |
| 34 | * |
| 35 | * @access public |
| 36 | * @param string |
| 37 | * @param integer |
| 38 | * @param string the end character. Usually an ellipsis |
| 39 | * @return string |
| 40 | */ |
| 41 | if ( ! function_exists('word_limiter')) |
| 42 | { |
| 43 | function word_limiter($str, $limit = 100, $end_char = '…') |
| 44 | { |
| 45 | if (trim($str) == '') |
| 46 | { |
| 47 | return $str; |
| 48 | } |
| 49 | |
| 50 | preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches); |
| 51 | |
| 52 | if (strlen($str) == strlen($matches[0])) |
| 53 | { |
| 54 | $end_char = ''; |
| 55 | } |
| 56 | |
| 57 | return rtrim($matches[0]).$end_char; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // ------------------------------------------------------------------------ |
| 62 | |
| 63 | /** |
| 64 | * Character Limiter |
| 65 | * |
| 66 | * Limits the string based on the character count. Preserves complete words |
| 67 | * so the character count may not be exactly as specified. |
| 68 | * |
| 69 | * @access public |
| 70 | * @param string |
| 71 | * @param integer |
| 72 | * @param string the end character. Usually an ellipsis |
| 73 | * @return string |
| 74 | */ |
| 75 | if ( ! function_exists('character_limiter')) |
| 76 | { |
| 77 | function character_limiter($str, $n = 500, $end_char = '…') |
| 78 | { |
| 79 | if (strlen($str) < $n) |
| 80 | { |
| 81 | return $str; |
| 82 | } |
| 83 | |
| 84 | $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str)); |
| 85 | |
| 86 | if (strlen($str) <= $n) |
| 87 | { |
| 88 | return $str; |
| 89 | } |
Derek Jones | 01d6b4f | 2009-02-03 14:51:00 +0000 | [diff] [blame^] | 90 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | $out = ""; |
| 92 | foreach (explode(' ', trim($str)) as $val) |
| 93 | { |
Derek Jones | 01d6b4f | 2009-02-03 14:51:00 +0000 | [diff] [blame^] | 94 | $out .= $val.' '; |
| 95 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | if (strlen($out) >= $n) |
| 97 | { |
Derek Jones | 01d6b4f | 2009-02-03 14:51:00 +0000 | [diff] [blame^] | 98 | $out = trim($out); |
| 99 | return (strlen($out) == strlen($str)) ? $out : $out.$end_char; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // ------------------------------------------------------------------------ |
| 106 | |
| 107 | /** |
| 108 | * High ASCII to Entities |
| 109 | * |
| 110 | * Converts High ascii text and MS Word special characters to character entities |
| 111 | * |
| 112 | * @access public |
| 113 | * @param string |
| 114 | * @return string |
| 115 | */ |
| 116 | if ( ! function_exists('ascii_to_entities')) |
| 117 | { |
| 118 | function ascii_to_entities($str) |
| 119 | { |
| 120 | $count = 1; |
| 121 | $out = ''; |
| 122 | $temp = array(); |
| 123 | |
| 124 | for ($i = 0, $s = strlen($str); $i < $s; $i++) |
| 125 | { |
| 126 | $ordinal = ord($str[$i]); |
| 127 | |
| 128 | if ($ordinal < 128) |
| 129 | { |
| 130 | $out .= $str[$i]; |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | if (count($temp) == 0) |
| 135 | { |
| 136 | $count = ($ordinal < 224) ? 2 : 3; |
| 137 | } |
| 138 | |
| 139 | $temp[] = $ordinal; |
| 140 | |
| 141 | if (count($temp) == $count) |
| 142 | { |
| 143 | $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); |
| 144 | |
| 145 | $out .= '&#'.$number.';'; |
| 146 | $count = 1; |
| 147 | $temp = array(); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return $out; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // ------------------------------------------------------------------------ |
| 157 | |
| 158 | /** |
| 159 | * Entities to ASCII |
| 160 | * |
| 161 | * Converts character entities back to ASCII |
| 162 | * |
| 163 | * @access public |
| 164 | * @param string |
| 165 | * @param bool |
| 166 | * @return string |
| 167 | */ |
| 168 | if ( ! function_exists('entities_to_ascii')) |
| 169 | { |
| 170 | function entities_to_ascii($str, $all = TRUE) |
| 171 | { |
| 172 | if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) |
| 173 | { |
| 174 | for ($i = 0, $s = count($matches['0']); $i < $s; $i++) |
| 175 | { |
| 176 | $digits = $matches['1'][$i]; |
| 177 | |
| 178 | $out = ''; |
| 179 | |
| 180 | if ($digits < 128) |
| 181 | { |
| 182 | $out .= chr($digits); |
| 183 | |
| 184 | } |
| 185 | elseif ($digits < 2048) |
| 186 | { |
| 187 | $out .= chr(192 + (($digits - ($digits % 64)) / 64)); |
| 188 | $out .= chr(128 + ($digits % 64)); |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); |
| 193 | $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); |
| 194 | $out .= chr(128 + ($digits % 64)); |
| 195 | } |
| 196 | |
| 197 | $str = str_replace($matches['0'][$i], $out, $str); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if ($all) |
| 202 | { |
| 203 | $str = str_replace(array("&", "<", ">", """, "'", "-"), |
| 204 | array("&","<",">","\"", "'", "-"), |
| 205 | $str); |
| 206 | } |
| 207 | |
| 208 | return $str; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // ------------------------------------------------------------------------ |
| 213 | |
| 214 | /** |
| 215 | * Word Censoring Function |
| 216 | * |
| 217 | * Supply a string and an array of disallowed words and any |
| 218 | * matched words will be converted to #### or to the replacement |
| 219 | * word you've submitted. |
| 220 | * |
| 221 | * @access public |
| 222 | * @param string the text string |
| 223 | * @param string the array of censoered words |
| 224 | * @param string the optional replacement value |
| 225 | * @return string |
| 226 | */ |
| 227 | if ( ! function_exists('word_censor')) |
| 228 | { |
| 229 | function word_censor($str, $censored, $replacement = '') |
| 230 | { |
| 231 | if ( ! is_array($censored)) |
| 232 | { |
| 233 | return $str; |
| 234 | } |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 235 | |
| 236 | $str = ' '.$str.' '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 238 | // \w, \b and a few others do not match on a unicode character |
| 239 | // set for performance reasons. As a result words like über |
| 240 | // will not match on a word boundary. Instead, we'll assume that |
Derek Jones | 01d6b4f | 2009-02-03 14:51:00 +0000 | [diff] [blame^] | 241 | // a bad word will be bookended by any of these characters. |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 242 | $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]'; |
| 243 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 244 | foreach ($censored as $badword) |
| 245 | { |
| 246 | if ($replacement != '') |
| 247 | { |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 248 | $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/i", "\\1{$replacement}\\3", $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | } |
| 250 | else |
| 251 | { |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 252 | $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/ie", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'", $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 255 | |
| 256 | return trim($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | // ------------------------------------------------------------------------ |
| 261 | |
| 262 | /** |
| 263 | * Code Highlighter |
| 264 | * |
| 265 | * Colorizes code strings |
| 266 | * |
| 267 | * @access public |
| 268 | * @param string the text string |
| 269 | * @return string |
| 270 | */ |
| 271 | if ( ! function_exists('highlight_code')) |
| 272 | { |
| 273 | function highlight_code($str) |
| 274 | { |
| 275 | // The highlight string function encodes and highlights |
| 276 | // brackets so we need them to start raw |
| 277 | $str = str_replace(array('<', '>'), array('<', '>'), $str); |
| 278 | |
| 279 | // Replace any existing PHP tags to temporary markers so they don't accidentally |
| 280 | // break the string out of PHP, and thus, thwart the highlighting. |
| 281 | |
| 282 | $str = str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'), |
| 283 | array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str); |
| 284 | |
| 285 | // The highlight_string function requires that the text be surrounded |
| 286 | // by PHP tags, which we will remove later |
| 287 | $str = '<?php '.$str.' ?>'; // <? |
| 288 | |
| 289 | // All the magic happens here, baby! |
| 290 | $str = highlight_string($str, TRUE); |
| 291 | |
| 292 | // Prior to PHP 5, the highligh function used icky <font> tags |
| 293 | // so we'll replace them with <span> tags. |
| 294 | |
| 295 | if (abs(PHP_VERSION) < 5) |
| 296 | { |
| 297 | $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str); |
| 298 | $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); |
| 299 | } |
| 300 | |
| 301 | // Remove our artificially added PHP, and the syntax highlighting that came with it |
| 302 | $str = preg_replace('/<span style="color: #([A-Z0-9]+)"><\?php( | )/i', '<span style="color: #$1">', $str); |
| 303 | $str = preg_replace('/(<span style="color: #[A-Z0-9]+">.*?)\?><\/span>\n<\/span>\n<\/code>/is', "$1</span>\n</span>\n</code>", $str); |
| 304 | $str = preg_replace('/<span style="color: #[A-Z0-9]+"\><\/span>/i', '', $str); |
| 305 | |
| 306 | // Replace our markers back to PHP tags. |
| 307 | $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), |
| 308 | array('<?', '?>', '<%', '%>', '\\', '</script>'), $str); |
| 309 | |
| 310 | return $str; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // ------------------------------------------------------------------------ |
| 315 | |
| 316 | /** |
| 317 | * Phrase Highlighter |
| 318 | * |
| 319 | * Highlights a phrase within a text string |
| 320 | * |
| 321 | * @access public |
| 322 | * @param string the text string |
| 323 | * @param string the phrase you'd like to highlight |
| 324 | * @param string the openging tag to precede the phrase with |
| 325 | * @param string the closing tag to end the phrase with |
| 326 | * @return string |
| 327 | */ |
| 328 | if ( ! function_exists('highlight_phrase')) |
| 329 | { |
| 330 | function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>') |
| 331 | { |
| 332 | if ($str == '') |
| 333 | { |
| 334 | return ''; |
| 335 | } |
| 336 | |
| 337 | if ($phrase != '') |
| 338 | { |
| 339 | return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str); |
| 340 | } |
| 341 | |
| 342 | return $str; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | // ------------------------------------------------------------------------ |
| 347 | |
| 348 | /** |
| 349 | * Word Wrap |
| 350 | * |
| 351 | * Wraps text at the specified character. Maintains the integrity of words. |
| 352 | * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor |
| 353 | * will URLs. |
| 354 | * |
| 355 | * @access public |
| 356 | * @param string the text string |
| 357 | * @param integer the number of characters to wrap at |
| 358 | * @return string |
| 359 | */ |
| 360 | if ( ! function_exists('word_wrap')) |
| 361 | { |
| 362 | function word_wrap($str, $charlim = '76') |
| 363 | { |
| 364 | // Se the character limit |
| 365 | if ( ! is_numeric($charlim)) |
| 366 | $charlim = 76; |
| 367 | |
| 368 | // Reduce multiple spaces |
| 369 | $str = preg_replace("| +|", " ", $str); |
| 370 | |
| 371 | // Standardize newlines |
| 372 | if (strpos($str, "\r") !== FALSE) |
| 373 | { |
| 374 | $str = str_replace(array("\r\n", "\r"), "\n", $str); |
| 375 | } |
| 376 | |
| 377 | // If the current word is surrounded by {unwrap} tags we'll |
| 378 | // strip the entire chunk and replace it with a marker. |
| 379 | $unwrap = array(); |
| 380 | if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches)) |
| 381 | { |
| 382 | for ($i = 0; $i < count($matches['0']); $i++) |
| 383 | { |
| 384 | $unwrap[] = $matches['1'][$i]; |
| 385 | $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // Use PHP's native function to do the initial wordwrap. |
| 390 | // We set the cut flag to FALSE so that any individual words that are |
| 391 | // too long get left alone. In the next step we'll deal with them. |
| 392 | $str = wordwrap($str, $charlim, "\n", FALSE); |
| 393 | |
| 394 | // Split the string into individual lines of text and cycle through them |
| 395 | $output = ""; |
| 396 | foreach (explode("\n", $str) as $line) |
| 397 | { |
| 398 | // Is the line within the allowed character count? |
| 399 | // If so we'll join it to the output and continue |
| 400 | if (strlen($line) <= $charlim) |
| 401 | { |
| 402 | $output .= $line."\n"; |
| 403 | continue; |
| 404 | } |
| 405 | |
| 406 | $temp = ''; |
| 407 | while((strlen($line)) > $charlim) |
| 408 | { |
| 409 | // If the over-length word is a URL we won't wrap it |
| 410 | if (preg_match("!\[url.+\]|://|wwww.!", $line)) |
| 411 | { |
| 412 | break; |
| 413 | } |
| 414 | |
| 415 | // Trim the word down |
| 416 | $temp .= substr($line, 0, $charlim-1); |
| 417 | $line = substr($line, $charlim-1); |
| 418 | } |
| 419 | |
| 420 | // If $temp contains data it means we had to split up an over-length |
| 421 | // word into smaller chunks so we'll add it back to our current line |
| 422 | if ($temp != '') |
| 423 | { |
| 424 | $output .= $temp . "\n" . $line; |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | $output .= $line; |
| 429 | } |
| 430 | |
| 431 | $output .= "\n"; |
| 432 | } |
| 433 | |
| 434 | // Put our markers back |
| 435 | if (count($unwrap) > 0) |
| 436 | { |
| 437 | foreach ($unwrap as $key => $val) |
| 438 | { |
| 439 | $output = str_replace("{{unwrapped".$key."}}", $val, $output); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | // Remove the unwrap tags |
| 444 | $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output); |
| 445 | |
| 446 | return $output; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | |
| 451 | /* End of file text_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 452 | /* Location: ./system/helpers/text_helper.php */ |