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