blob: 2a42cba7ea776e568a0e8749c560484567e05959 [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 */
41function word_limiter($str, $n = 100, $end_char = '&#8230;')
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 */
78function character_limiter($str, $n = 500, $end_char = '&#8230;')
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 */
114function 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 */
163function 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("&amp;", "&lt;", "&gt;", "&quot;", "&apos;", "&#45;"),
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 */
219function 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 */
253function 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('&lt;', '&gt;'), 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
Derek Jones9468f3e2008-01-22 19:19:27 +0000262 $str = str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'),
263 array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str);
264
Derek Allard3d879d52008-01-18 19:41:32 +0000265 // The highlight_string function requires that the text be surrounded
266 // by PHP tags. Since we don't know if A) the submitted text has PHP tags,
267 // or B) whether the PHP tags enclose the entire string, we will add our
268 // own PHP tags around the string along with some markers to make replacement easier later
269
Derek Jonese3525492008-01-22 19:51:03 +0000270 $str = '<?php tempstart'."\n".$str.'tempend ?>';
Derek Allard3d879d52008-01-18 19:41:32 +0000271
272 // All the magic happens here, baby!
273 $str = highlight_string($str, TRUE);
274
275 // Prior to PHP 5, the highlight function used icky font tags
276 // so we'll replace them with span tags.
277 if (abs(phpversion()) < 5)
278 {
279 $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
280 $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
281 }
282
Derek Jones9468f3e2008-01-22 19:19:27 +0000283 // Remove our artificially added PHP and the empty span that results from our temp markers
Derek Jonese3525492008-01-22 19:51:03 +0000284 $str = preg_replace("#\<code\>.+?tempstart\<br />\</span\>#is", "<code>\n", $str);
285 $str = preg_replace("#\<code\>.+?tempstart\<br />#is", "<code>\n", $str);
286 $str = preg_replace("#tempend.+#is", "</span>\n</code>", $str);
Derek Allard3d879d52008-01-18 19:41:32 +0000287
288 // Replace our markers back to PHP tags.
Derek Jones9468f3e2008-01-22 19:19:27 +0000289 $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
290 array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'), $str);
291
Derek Allard3d879d52008-01-18 19:41:32 +0000292 return $str;
293}
294
295// ------------------------------------------------------------------------
296
297/**
298 * Phrase Highlighter
299 *
300 * Highlights a phrase within a text string
301 *
302 * @access public
303 * @param string the text string
304 * @param string the phrase you'd like to highlight
305 * @param string the openging tag to precede the phrase with
306 * @param string the closing tag to end the phrase with
307 * @return string
308 */
309function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
310{
311 if ($str == '')
312 {
313 return '';
314 }
315
316 if ($phrase != '')
317 {
318 return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);
319 }
320
321 return $str;
322}
323
324// ------------------------------------------------------------------------
325
326/**
327 * Word Wrap
328 *
329 * Wraps text at the specified character. Maintains the integrity of words.
330 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
331 * will URLs.
332 *
333 * @access public
334 * @param string the text string
335 * @param integer the number of characters to wrap at
336 * @return string
337 */
338function word_wrap($str, $charlim = '76')
339{
340 // Se the character limit
341 if ( ! is_numeric($charlim))
342 $charlim = 76;
343
344 // Reduce multiple spaces
345 $str = preg_replace("| +|", " ", $str);
346
347 // Standardize newlines
348 $str = preg_replace("/\r\n|\r/", "\n", $str);
349
350 // If the current word is surrounded by {unwrap} tags we'll
351 // strip the entire chunk and replace it with a marker.
352 $unwrap = array();
353 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
354 {
355 for ($i = 0; $i < count($matches['0']); $i++)
356 {
357 $unwrap[] = $matches['1'][$i];
358 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
359 }
360 }
361
362 // Use PHP's native function to do the initial wordwrap.
363 // We set the cut flag to FALSE so that any individual words that are
364 // too long get left alone. In the next step we'll deal with them.
365 $str = wordwrap($str, $charlim, "\n", FALSE);
366
367 // Split the string into individual lines of text and cycle through them
368 $output = "";
369 foreach (explode("\n", $str) as $line)
370 {
371 // Is the line within the allowed character count?
372 // If so we'll join it to the output and continue
373 if (strlen($line) <= $charlim)
374 {
375 $output .= $line."\n";
376 continue;
377 }
378
379 $temp = '';
380 while((strlen($line)) > $charlim)
381 {
382 // If the over-length word is a URL we won't wrap it
383 if (preg_match("!\[url.+\]|://|wwww.!", $line))
384 {
385 break;
386 }
387
388 // Trim the word down
389 $temp .= substr($line, 0, $charlim-1);
390 $line = substr($line, $charlim-1);
391 }
392
393 // If $temp contains data it means we had to split up an over-length
394 // word into smaller chunks so we'll add it back to our current line
395 if ($temp != '')
396 {
397 $output .= $temp . "\n" . $line;
398 }
399 else
400 {
401 $output .= $line;
402 }
403
404 $output .= "\n";
405 }
406
407 // Put our markers back
408 if (count($unwrap) > 0)
409 {
410 foreach ($unwrap as $key => $val)
411 {
412 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
413 }
414 }
415
416 // Remove the unwrap tags
417 $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
418
419 return $output;
420}
421
422
423?>