1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
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
16 // ------------------------------------------------------------------------
19 * CodeIgniter Text Helpers
21 * @package CodeIgniter
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/text_helper.html
28 // ------------------------------------------------------------------------
33 * Limits a string to X number of words.
38 * @param string the end character. Usually an ellipsis
41 if ( ! function_exists('word_limiter'))
43 function word_limiter($str, $limit = 100, $end_char = '…')
50 preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
52 if (strlen($str) == strlen($matches[0]))
57 return rtrim($matches[0]).$end_char;
61 // ------------------------------------------------------------------------
66 * Limits the string based on the character count. Preserves complete words
67 * so the character count may not be exactly as specified.
72 * @param string the end character. Usually an ellipsis
75 if ( ! function_exists('character_limiter'))
77 function character_limiter($str, $n = 500, $end_char = '…')
79 if (strlen($str) < $n)
84 $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
86 if (strlen($str) <= $n)
92 foreach (explode(' ', trim($str)) as $val)
95 if (strlen($out) >= $n)
97 return trim($out).$end_char;
103 // ------------------------------------------------------------------------
106 * High ASCII to Entities
108 * Converts High ascii text and MS Word special characters to character entities
114 if ( ! function_exists('ascii_to_entities'))
116 function ascii_to_entities($str)
122 for ($i = 0, $s = strlen($str); $i < $s; $i++)
124 $ordinal = ord($str[$i]);
132 if (count($temp) == 0)
134 $count = ($ordinal < 224) ? 2 : 3;
139 if (count($temp) == $count)
141 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
143 $out .= '&#'.$number.';';
154 // ------------------------------------------------------------------------
159 * Converts character entities back to ASCII
166 if ( ! function_exists('entities_to_ascii'))
168 function entities_to_ascii($str, $all = TRUE)
170 if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
172 for ($i = 0, $s = count($matches['0']); $i < $s; $i++)
174 $digits = $matches['1'][$i];
180 $out .= chr($digits);
183 elseif ($digits < 2048)
185 $out .= chr(192 + (($digits - ($digits % 64)) / 64));
186 $out .= chr(128 + ($digits % 64));
190 $out .= chr(224 + (($digits - ($digits % 4096)) / 4096));
191 $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64));
192 $out .= chr(128 + ($digits % 64));
195 $str = str_replace($matches['0'][$i], $out, $str);
201 $str = str_replace(array("&", "<", ">", """, "'", "-"),
202 array("&","<",">","\"", "'", "-"),
210 // ------------------------------------------------------------------------
213 * Word Censoring Function
215 * Supply a string and an array of disallowed words and any
216 * matched words will be converted to #### or to the replacement
217 * word you've submitted.
220 * @param string the text string
221 * @param string the array of censoered words
222 * @param string the optional replacement value
225 if ( ! function_exists('word_censor'))
227 function word_censor($str, $censored, $replacement = '')
229 if ( ! is_array($censored))
235 foreach ($censored as $badword)
237 if ($replacement != '')
239 $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/i", $replacement, $str);
243 $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")\b/ie", "str_repeat('#', strlen('\\1'))", $str);
251 // ------------------------------------------------------------------------
256 * Colorizes code strings
259 * @param string the text string
262 if ( ! function_exists('highlight_code'))
264 function highlight_code($str)
266 // The highlight string function encodes and highlights
267 // brackets so we need them to start raw
268 $str = str_replace(array('<', '>'), array('<', '>'), $str);
270 // Replace any existing PHP tags to temporary markers so they don't accidentally
271 // break the string out of PHP, and thus, thwart the highlighting.
273 $str = str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'),
274 array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str);
276 // The highlight_string function requires that the text be surrounded
277 // by PHP tags, which we will remove later
278 $str = '<?php '.$str.' ?>'; // <?
280 // All the magic happens here, baby!
281 $str = highlight_string($str, TRUE);
283 // Prior to PHP 5, the highligh function used icky <font> tags
284 // so we'll replace them with <span> tags.
286 if (abs(PHP_VERSION) < 5)
288 $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
289 $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
292 // Remove our artificially added PHP, and the syntax highlighting that came with it
293 $str = preg_replace('/<span style="color: #([A-Z0-9]+)"><\?php( | )/i', '<span style="color: #$1">', $str);
294 $str = preg_replace('/(<span style="color: #[A-Z0-9]+">.*?)\?><\/span>\n<\/span>\n<\/code>/is', "$1</span>\n</span>\n</code>", $str);
295 $str = preg_replace('/<span style="color: #[A-Z0-9]+"\><\/span>/i', '', $str);
297 // Replace our markers back to PHP tags.
298 $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
299 array('<?', '?>', '<%', '%>', '\\', '</script>'), $str);
305 // ------------------------------------------------------------------------
310 * Highlights a phrase within a text string
313 * @param string the text string
314 * @param string the phrase you'd like to highlight
315 * @param string the openging tag to precede the phrase with
316 * @param string the closing tag to end the phrase with
319 if ( ! function_exists('highlight_phrase'))
321 function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
330 return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);
337 // ------------------------------------------------------------------------
342 * Wraps text at the specified character. Maintains the integrity of words.
343 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
347 * @param string the text string
348 * @param integer the number of characters to wrap at
351 if ( ! function_exists('word_wrap'))
353 function word_wrap($str, $charlim = '76')
355 // Se the character limit
356 if ( ! is_numeric($charlim))
359 // Reduce multiple spaces
360 $str = preg_replace("| +|", " ", $str);
362 // Standardize newlines
363 if (strpos($str, "\r") !== FALSE)
365 $str = str_replace(array("\r\n", "\r"), "\n", $str);
368 // If the current word is surrounded by {unwrap} tags we'll
369 // strip the entire chunk and replace it with a marker.
371 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
373 for ($i = 0; $i < count($matches['0']); $i++)
375 $unwrap[] = $matches['1'][$i];
376 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
380 // Use PHP's native function to do the initial wordwrap.
381 // We set the cut flag to FALSE so that any individual words that are
382 // too long get left alone. In the next step we'll deal with them.
383 $str = wordwrap($str, $charlim, "\n", FALSE);
385 // Split the string into individual lines of text and cycle through them
387 foreach (explode("\n", $str) as $line)
389 // Is the line within the allowed character count?
390 // If so we'll join it to the output and continue
391 if (strlen($line) <= $charlim)
393 $output .= $line."\n";
398 while((strlen($line)) > $charlim)
400 // If the over-length word is a URL we won't wrap it
401 if (preg_match("!\[url.+\]|://|wwww.!", $line))
406 // Trim the word down
407 $temp .= substr($line, 0, $charlim-1);
408 $line = substr($line, $charlim-1);
411 // If $temp contains data it means we had to split up an over-length
412 // word into smaller chunks so we'll add it back to our current line
415 $output .= $temp . "\n" . $line;
425 // Put our markers back
426 if (count($unwrap) > 0)
428 foreach ($unwrap as $key => $val)
430 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
434 // Remove the unwrap tags
435 $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
442 /* End of file text_helper.php */
443 /* Location: ./system/helpers/text_helper.php */