blob: 9f13896ae4bd1c1bf2ed6ac843b6a1b46ca8f17e [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard3d879d52008-01-18 19:41:32 +00002/**
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
Rick Ellis86d721c2008-09-12 23:33:40 +00009 * @copyright Copyright (c) 2008, 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 Jones0b59f272008-05-13 04:22:33 +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 Jones0b59f272008-05-13 04:22:33 +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
Derek Jones0b59f272008-05-13 04:22:33 +000084 $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
Derek Jones269b9422008-01-28 21:00:20 +000085
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 Jones0b59f272008-05-13 04:22:33 +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 Jones0b59f272008-05-13 04:22:33 +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 Jones0b59f272008-05-13 04:22:33 +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 Jones0b59f272008-05-13 04:22:33 +0000229 if ( ! is_array($censored))
Derek Jones269b9422008-01-28 21:00:20 +0000230 {
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 Jones0b59f272008-05-13 04:22:33 +0000262if ( ! function_exists('highlight_code'))
Derek Jones269b9422008-01-28 21:00:20 +0000263{
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
Derek Jonesc0e72b02008-10-03 18:56:06 +0000277 // by PHP tags, which we will remove later
278 $str = '<?php '.$str.' ?>'; // <?
279
280 // All the magic happens here, baby!
Derek Jones269b9422008-01-28 21:00:20 +0000281 $str = highlight_string($str, TRUE);
Derek Allard3d879d52008-01-18 19:41:32 +0000282
Derek Jonesc0e72b02008-10-03 18:56:06 +0000283 // Prior to PHP 5, the highligh function used icky <font> tags
284 // so we'll replace them with <span> tags.
285
286 if (abs(PHP_VERSION) < 5)
Derek Jones269b9422008-01-28 21:00:20 +0000287 {
288 $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
289 $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
290 }
Derek Jones337c74a2008-10-03 19:09:53 +0000291
Derek Jonesc0e72b02008-10-03 18:56:06 +0000292 // Remove our artificially added PHP, and the syntax highlighting that came with it
Derek Jones337c74a2008-10-03 19:09:53 +0000293 $str = preg_replace('/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i', '<span style="color: #$1">', $str);
294 $str = preg_replace('/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is', "$1</span>\n</span>\n</code>", $str);
Derek Jonesc0e72b02008-10-03 18:56:06 +0000295 $str = preg_replace('/<span style="color: #[A-Z0-9]+"\><\/span>/i', '', $str);
296
Derek Jones269b9422008-01-28 21:00:20 +0000297 // Replace our markers back to PHP tags.
298 $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
299 array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'), $str);
Derek Jones9468f3e2008-01-22 19:19:27 +0000300
Derek Jones269b9422008-01-28 21:00:20 +0000301 return $str;
302 }
Derek Allard3d879d52008-01-18 19:41:32 +0000303}
304
305// ------------------------------------------------------------------------
306
307/**
308 * Phrase Highlighter
309 *
310 * Highlights a phrase within a text string
311 *
312 * @access public
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
317 * @return string
318 */
Derek Jones0b59f272008-05-13 04:22:33 +0000319if ( ! function_exists('highlight_phrase'))
Derek Allard3d879d52008-01-18 19:41:32 +0000320{
Derek Jones269b9422008-01-28 21:00:20 +0000321 function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
Derek Allard3d879d52008-01-18 19:41:32 +0000322 {
Derek Jones269b9422008-01-28 21:00:20 +0000323 if ($str == '')
324 {
325 return '';
326 }
Derek Allard3d879d52008-01-18 19:41:32 +0000327
Derek Jones269b9422008-01-28 21:00:20 +0000328 if ($phrase != '')
329 {
330 return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);
331 }
Derek Allard3d879d52008-01-18 19:41:32 +0000332
Derek Jones269b9422008-01-28 21:00:20 +0000333 return $str;
334 }
Derek Allard3d879d52008-01-18 19:41:32 +0000335}
336
337// ------------------------------------------------------------------------
338
339/**
340 * Word Wrap
341 *
342 * Wraps text at the specified character. Maintains the integrity of words.
343 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
344 * will URLs.
345 *
346 * @access public
347 * @param string the text string
348 * @param integer the number of characters to wrap at
349 * @return string
350 */
Derek Jones0b59f272008-05-13 04:22:33 +0000351if ( ! function_exists('word_wrap'))
Derek Allard3d879d52008-01-18 19:41:32 +0000352{
Derek Jones269b9422008-01-28 21:00:20 +0000353 function word_wrap($str, $charlim = '76')
Derek Allard3d879d52008-01-18 19:41:32 +0000354 {
Derek Jones269b9422008-01-28 21:00:20 +0000355 // Se the character limit
Derek Jones0b59f272008-05-13 04:22:33 +0000356 if ( ! is_numeric($charlim))
Derek Jones269b9422008-01-28 21:00:20 +0000357 $charlim = 76;
Derek Allard3d879d52008-01-18 19:41:32 +0000358
Derek Jones269b9422008-01-28 21:00:20 +0000359 // Reduce multiple spaces
360 $str = preg_replace("| +|", " ", $str);
Derek Allard3d879d52008-01-18 19:41:32 +0000361
Derek Jones269b9422008-01-28 21:00:20 +0000362 // Standardize newlines
Derek Jones0b59f272008-05-13 04:22:33 +0000363 if (strpos($str, "\r") !== FALSE)
364 {
365 $str = str_replace(array("\r\n", "\r"), "\n", $str);
366 }
Derek Jones269b9422008-01-28 21:00:20 +0000367
368 // If the current word is surrounded by {unwrap} tags we'll
369 // strip the entire chunk and replace it with a marker.
370 $unwrap = array();
371 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
Derek Allard3d879d52008-01-18 19:41:32 +0000372 {
Derek Jones269b9422008-01-28 21:00:20 +0000373 for ($i = 0; $i < count($matches['0']); $i++)
Derek Allard3d879d52008-01-18 19:41:32 +0000374 {
Derek Jones269b9422008-01-28 21:00:20 +0000375 $unwrap[] = $matches['1'][$i];
376 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
377 }
378 }
379
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);
384
385 // Split the string into individual lines of text and cycle through them
386 $output = "";
387 foreach (explode("\n", $str) as $line)
388 {
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)
392 {
393 $output .= $line."\n";
394 continue;
395 }
396
397 $temp = '';
398 while((strlen($line)) > $charlim)
399 {
400 // If the over-length word is a URL we won't wrap it
401 if (preg_match("!\[url.+\]|://|wwww.!", $line))
402 {
403 break;
404 }
405
406 // Trim the word down
407 $temp .= substr($line, 0, $charlim-1);
408 $line = substr($line, $charlim-1);
409 }
410
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
413 if ($temp != '')
414 {
415 $output .= $temp . "\n" . $line;
416 }
417 else
418 {
419 $output .= $line;
Derek Allard3d879d52008-01-18 19:41:32 +0000420 }
421
Derek Jones269b9422008-01-28 21:00:20 +0000422 $output .= "\n";
Derek Allard3d879d52008-01-18 19:41:32 +0000423 }
424
Derek Jones269b9422008-01-28 21:00:20 +0000425 // Put our markers back
426 if (count($unwrap) > 0)
427 {
428 foreach ($unwrap as $key => $val)
429 {
430 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
431 }
432 }
433
434 // Remove the unwrap tags
435 $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
436
437 return $output;
Derek Allard3d879d52008-01-18 19:41:32 +0000438 }
Derek Allard3d879d52008-01-18 19:41:32 +0000439}
Derek Allard3d879d52008-01-18 19:41:32 +0000440
Derek Jones0b59f272008-05-13 04:22:33 +0000441
442/* End of file text_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000443/* Location: ./system/helpers/text_helper.php */