blob: 21ab77830b2ef95890352ab62fb0336e255b9921 [file] [log] [blame]
Derek Allard3d879d52008-01-18 19:41:32 +00001<?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 Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allard3d879d52008-01-18 19:41:32 +000012 * @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 Jones7a9193a2008-01-21 18:39:20 +000025 * @link http://codeigniter.com/user_guide/helpers/text_helper.html
Derek Allard3d879d52008-01-18 19:41:32 +000026 */
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 */
Derek Jones269b9422008-01-28 21:00:20 +000041if (! function_exists('word_limiter'))
Derek Allard3d879d52008-01-18 19:41:32 +000042{
Derek Jones2d87b4d2008-01-30 15:19:53 +000043 function word_limiter($str, $limit = 100, $end_char = '&#8230;')
Derek Allard3d879d52008-01-18 19:41:32 +000044 {
Derek Jones2d87b4d2008-01-30 15:19:53 +000045 if (trim($str) == '')
Derek Jones269b9422008-01-28 21:00:20 +000046 {
47 return $str;
48 }
Derek Allard3d879d52008-01-18 19:41:32 +000049
Derek Jones2d87b4d2008-01-30 15:19:53 +000050 preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
Derek Allard3d879d52008-01-18 19:41:32 +000051
Derek Jones2d87b4d2008-01-30 15:19:53 +000052 if (strlen($str) == strlen($matches[0]))
Derek Jones269b9422008-01-28 21:00:20 +000053 {
Derek Jones2d87b4d2008-01-30 15:19:53 +000054 $end_char = '';
Derek Jones269b9422008-01-28 21:00:20 +000055 }
Derek Jones2d87b4d2008-01-30 15:19:53 +000056
57 return rtrim($matches[0]).$end_char;
Derek Jones269b9422008-01-28 21:00:20 +000058 }
Derek Allard3d879d52008-01-18 19:41:32 +000059}
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 */
Derek Jones269b9422008-01-28 21:00:20 +000075if (! function_exists('character_limiter'))
Derek Allard3d879d52008-01-18 19:41:32 +000076{
Derek Jones269b9422008-01-28 21:00:20 +000077 function character_limiter($str, $n = 500, $end_char = '&#8230;')
Derek Allard3d879d52008-01-18 19:41:32 +000078 {
Derek Jones269b9422008-01-28 21:00:20 +000079 if (strlen($str) < $n)
Derek Allard3d879d52008-01-18 19:41:32 +000080 {
Derek Jones269b9422008-01-28 21:00:20 +000081 return $str;
82 }
83
84 $str = preg_replace("/\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", " ", $str));
85
86 if (strlen($str) <= $n)
87 {
88 return $str;
89 }
90
91 $out = "";
92 foreach (explode(' ', trim($str)) as $val)
93 {
94 $out .= $val.' ';
95 if (strlen($out) >= $n)
96 {
97 return trim($out).$end_char;
98 }
99 }
Derek Allard3d879d52008-01-18 19:41:32 +0000100 }
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 */
Derek Jones269b9422008-01-28 21:00:20 +0000114if (! function_exists('ascii_to_entities'))
Derek Allard3d879d52008-01-18 19:41:32 +0000115{
Derek Jones269b9422008-01-28 21:00:20 +0000116 function ascii_to_entities($str)
117 {
118 $count = 1;
119 $out = '';
120 $temp = array();
Derek Allard3d879d52008-01-18 19:41:32 +0000121
Derek Jones269b9422008-01-28 21:00:20 +0000122 for ($i = 0, $s = strlen($str); $i < $s; $i++)
123 {
124 $ordinal = ord($str[$i]);
Derek Allard3d879d52008-01-18 19:41:32 +0000125
Derek Jones269b9422008-01-28 21:00:20 +0000126 if ($ordinal < 128)
Derek Allard3d879d52008-01-18 19:41:32 +0000127 {
Derek Jones269b9422008-01-28 21:00:20 +0000128 $out .= $str[$i];
Derek Allard3d879d52008-01-18 19:41:32 +0000129 }
Derek Jones269b9422008-01-28 21:00:20 +0000130 else
Derek Allard3d879d52008-01-18 19:41:32 +0000131 {
Derek Jones269b9422008-01-28 21:00:20 +0000132 if (count($temp) == 0)
133 {
134 $count = ($ordinal < 224) ? 2 : 3;
135 }
136
137 $temp[] = $ordinal;
138
139 if (count($temp) == $count)
140 {
141 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
Derek Allard3d879d52008-01-18 19:41:32 +0000142
Derek Jones269b9422008-01-28 21:00:20 +0000143 $out .= '&#'.$number.';';
144 $count = 1;
145 $temp = array();
146 }
Derek Allard3d879d52008-01-18 19:41:32 +0000147 }
148 }
Derek Allard3d879d52008-01-18 19:41:32 +0000149
Derek Jones269b9422008-01-28 21:00:20 +0000150 return $out;
151 }
Derek Allard3d879d52008-01-18 19:41:32 +0000152}
153
154// ------------------------------------------------------------------------
155
156/**
157 * Entities to ASCII
158 *
159 * Converts character entities back to ASCII
160 *
161 * @access public
162 * @param string
163 * @param bool
164 * @return string
165 */
Derek Jones269b9422008-01-28 21:00:20 +0000166if (! function_exists('entities_to_ascii'))
Derek Allard3d879d52008-01-18 19:41:32 +0000167{
Derek Jones269b9422008-01-28 21:00:20 +0000168 function entities_to_ascii($str, $all = TRUE)
169 {
170 if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
171 {
172 for ($i = 0, $s = count($matches['0']); $i < $s; $i++)
173 {
174 $digits = $matches['1'][$i];
Derek Allard3d879d52008-01-18 19:41:32 +0000175
Derek Jones269b9422008-01-28 21:00:20 +0000176 $out = '';
Derek Allard3d879d52008-01-18 19:41:32 +0000177
Derek Jones269b9422008-01-28 21:00:20 +0000178 if ($digits < 128)
179 {
180 $out .= chr($digits);
Derek Allard3d879d52008-01-18 19:41:32 +0000181
Derek Jones269b9422008-01-28 21:00:20 +0000182 }
183 elseif ($digits < 2048)
184 {
185 $out .= chr(192 + (($digits - ($digits % 64)) / 64));
186 $out .= chr(128 + ($digits % 64));
187 }
188 else
189 {
190 $out .= chr(224 + (($digits - ($digits % 4096)) / 4096));
191 $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64));
192 $out .= chr(128 + ($digits % 64));
193 }
Derek Allard3d879d52008-01-18 19:41:32 +0000194
Derek Jones269b9422008-01-28 21:00:20 +0000195 $str = str_replace($matches['0'][$i], $out, $str);
196 }
Derek Allard3d879d52008-01-18 19:41:32 +0000197 }
Derek Allard3d879d52008-01-18 19:41:32 +0000198
Derek Jones269b9422008-01-28 21:00:20 +0000199 if ($all)
200 {
201 $str = str_replace(array("&amp;", "&lt;", "&gt;", "&quot;", "&apos;", "&#45;"),
202 array("&","<",">","\"", "'", "-"),
203 $str);
204 }
Derek Allard3d879d52008-01-18 19:41:32 +0000205
Derek Jones269b9422008-01-28 21:00:20 +0000206 return $str;
207 }
Derek Allard3d879d52008-01-18 19:41:32 +0000208}
209
210// ------------------------------------------------------------------------
211
212/**
213 * Word Censoring Function
214 *
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.
218 *
219 * @access public
220 * @param string the text string
221 * @param string the array of censoered words
222 * @param string the optional replacement value
223 * @return string
224 */
Derek Jones269b9422008-01-28 21:00:20 +0000225if (! function_exists('word_censor'))
Derek Allard3d879d52008-01-18 19:41:32 +0000226{
Derek Jones269b9422008-01-28 21:00:20 +0000227 function word_censor($str, $censored, $replacement = '')
Derek Allard3d879d52008-01-18 19:41:32 +0000228 {
Derek Jones269b9422008-01-28 21:00:20 +0000229 if ( ! is_array($censored))
230 {
231 return $str;
232 }
Derek Allard3d879d52008-01-18 19:41:32 +0000233
Derek Jones269b9422008-01-28 21:00:20 +0000234 $str = ' '.$str.' ';
235 foreach ($censored as $badword)
Derek Allard3d879d52008-01-18 19:41:32 +0000236 {
Derek Jones269b9422008-01-28 21:00:20 +0000237 if ($replacement != '')
238 {
239 $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str);
240 }
241 else
242 {
243 $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str);
244 }
Derek Allard3d879d52008-01-18 19:41:32 +0000245 }
Derek Allard3d879d52008-01-18 19:41:32 +0000246
Derek Jones269b9422008-01-28 21:00:20 +0000247 return trim($str);
248 }
Derek Allard3d879d52008-01-18 19:41:32 +0000249}
250
251// ------------------------------------------------------------------------
252
253/**
254 * Code Highlighter
255 *
256 * Colorizes code strings
257 *
258 * @access public
259 * @param string the text string
260 * @return string
261 */
Derek Jones269b9422008-01-28 21:00:20 +0000262if (! function_exists('highlight_code'))
263{
264 function highlight_code($str)
265 {
266 // The highlight string function encodes and highlights
267 // brackets so we need them to start raw
268 $str = str_replace(array('&lt;', '&gt;'), array('<', '>'), $str);
Derek Allard3d879d52008-01-18 19:41:32 +0000269
Derek Jones269b9422008-01-28 21:00:20 +0000270 // 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.
Derek Allard3d879d52008-01-18 19:41:32 +0000272
Derek Jones269b9422008-01-28 21:00:20 +0000273 $str = str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'),
274 array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str);
Derek Jones9468f3e2008-01-22 19:19:27 +0000275
Derek Jones269b9422008-01-28 21:00:20 +0000276 // The highlight_string function requires that the text be surrounded
277 // by PHP tags. Since we don't know if A) the submitted text has PHP tags,
278 // or B) whether the PHP tags enclose the entire string, we will add our
279 // own PHP tags around the string along with some markers to make replacement easier later
Derek Allard3d879d52008-01-18 19:41:32 +0000280
Derek Jones269b9422008-01-28 21:00:20 +0000281 $str = '<?php tempstart'."\n".$str.'tempend ?>';
Derek Allard3d879d52008-01-18 19:41:32 +0000282
Derek Jones269b9422008-01-28 21:00:20 +0000283 // All the magic happens here, baby!
284 $str = highlight_string($str, TRUE);
Derek Allard3d879d52008-01-18 19:41:32 +0000285
Derek Jones269b9422008-01-28 21:00:20 +0000286 // Prior to PHP 5, the highlight function used icky font tags
287 // so we'll replace them with span tags.
288 if (abs(phpversion()) < 5)
289 {
290 $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
291 $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
292 }
Derek Allard3d879d52008-01-18 19:41:32 +0000293
Derek Jones269b9422008-01-28 21:00:20 +0000294 // Remove our artificially added PHP
295 $str = preg_replace("#\<code\>.+?tempstart\<br />(?:\</span\>)?#is", "<code>\n", $str);
296 $str = preg_replace("#tempend.+#is", "</span>\n</code>", $str);
Derek Allard3d879d52008-01-18 19:41:32 +0000297
Derek Jones269b9422008-01-28 21:00:20 +0000298 // Replace our markers back to PHP tags.
299 $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
300 array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'), $str);
Derek Jones9468f3e2008-01-22 19:19:27 +0000301
Derek Jones269b9422008-01-28 21:00:20 +0000302 return $str;
303 }
Derek Allard3d879d52008-01-18 19:41:32 +0000304}
305
306// ------------------------------------------------------------------------
307
308/**
309 * Phrase Highlighter
310 *
311 * Highlights a phrase within a text string
312 *
313 * @access public
314 * @param string the text string
315 * @param string the phrase you'd like to highlight
316 * @param string the openging tag to precede the phrase with
317 * @param string the closing tag to end the phrase with
318 * @return string
319 */
Derek Jones269b9422008-01-28 21:00:20 +0000320if (! function_exists('highlight_phrase'))
Derek Allard3d879d52008-01-18 19:41:32 +0000321{
Derek Jones269b9422008-01-28 21:00:20 +0000322 function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
Derek Allard3d879d52008-01-18 19:41:32 +0000323 {
Derek Jones269b9422008-01-28 21:00:20 +0000324 if ($str == '')
325 {
326 return '';
327 }
Derek Allard3d879d52008-01-18 19:41:32 +0000328
Derek Jones269b9422008-01-28 21:00:20 +0000329 if ($phrase != '')
330 {
331 return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);
332 }
Derek Allard3d879d52008-01-18 19:41:32 +0000333
Derek Jones269b9422008-01-28 21:00:20 +0000334 return $str;
335 }
Derek Allard3d879d52008-01-18 19:41:32 +0000336}
337
338// ------------------------------------------------------------------------
339
340/**
341 * Word Wrap
342 *
343 * Wraps text at the specified character. Maintains the integrity of words.
344 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
345 * will URLs.
346 *
347 * @access public
348 * @param string the text string
349 * @param integer the number of characters to wrap at
350 * @return string
351 */
Derek Jones269b9422008-01-28 21:00:20 +0000352if (! function_exists('word_wrap'))
Derek Allard3d879d52008-01-18 19:41:32 +0000353{
Derek Jones269b9422008-01-28 21:00:20 +0000354 function word_wrap($str, $charlim = '76')
Derek Allard3d879d52008-01-18 19:41:32 +0000355 {
Derek Jones269b9422008-01-28 21:00:20 +0000356 // Se the character limit
357 if ( ! is_numeric($charlim))
358 $charlim = 76;
Derek Allard3d879d52008-01-18 19:41:32 +0000359
Derek Jones269b9422008-01-28 21:00:20 +0000360 // Reduce multiple spaces
361 $str = preg_replace("| +|", " ", $str);
Derek Allard3d879d52008-01-18 19:41:32 +0000362
Derek Jones269b9422008-01-28 21:00:20 +0000363 // Standardize newlines
364 $str = preg_replace("/\r\n|\r/", "\n", $str);
365
366 // If the current word is surrounded by {unwrap} tags we'll
367 // strip the entire chunk and replace it with a marker.
368 $unwrap = array();
369 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
Derek Allard3d879d52008-01-18 19:41:32 +0000370 {
Derek Jones269b9422008-01-28 21:00:20 +0000371 for ($i = 0; $i < count($matches['0']); $i++)
Derek Allard3d879d52008-01-18 19:41:32 +0000372 {
Derek Jones269b9422008-01-28 21:00:20 +0000373 $unwrap[] = $matches['1'][$i];
374 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
375 }
376 }
377
378 // Use PHP's native function to do the initial wordwrap.
379 // We set the cut flag to FALSE so that any individual words that are
380 // too long get left alone. In the next step we'll deal with them.
381 $str = wordwrap($str, $charlim, "\n", FALSE);
382
383 // Split the string into individual lines of text and cycle through them
384 $output = "";
385 foreach (explode("\n", $str) as $line)
386 {
387 // Is the line within the allowed character count?
388 // If so we'll join it to the output and continue
389 if (strlen($line) <= $charlim)
390 {
391 $output .= $line."\n";
392 continue;
393 }
394
395 $temp = '';
396 while((strlen($line)) > $charlim)
397 {
398 // If the over-length word is a URL we won't wrap it
399 if (preg_match("!\[url.+\]|://|wwww.!", $line))
400 {
401 break;
402 }
403
404 // Trim the word down
405 $temp .= substr($line, 0, $charlim-1);
406 $line = substr($line, $charlim-1);
407 }
408
409 // If $temp contains data it means we had to split up an over-length
410 // word into smaller chunks so we'll add it back to our current line
411 if ($temp != '')
412 {
413 $output .= $temp . "\n" . $line;
414 }
415 else
416 {
417 $output .= $line;
Derek Allard3d879d52008-01-18 19:41:32 +0000418 }
419
Derek Jones269b9422008-01-28 21:00:20 +0000420 $output .= "\n";
Derek Allard3d879d52008-01-18 19:41:32 +0000421 }
422
Derek Jones269b9422008-01-28 21:00:20 +0000423 // Put our markers back
424 if (count($unwrap) > 0)
425 {
426 foreach ($unwrap as $key => $val)
427 {
428 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
429 }
430 }
431
432 // Remove the unwrap tags
433 $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
434
435 return $output;
Derek Allard3d879d52008-01-18 19:41:32 +0000436 }
Derek Allard3d879d52008-01-18 19:41:32 +0000437}
Derek Allard3d879d52008-01-18 19:41:32 +0000438
439?>