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