blob: 38b46e2484b1a06c114f8bb767738f3c927900a9 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter Text Helpers
32 *
33 * @package CodeIgniter
34 * @subpackage Helpers
35 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/helpers/text_helper.html
38 */
39
40// ------------------------------------------------------------------------
41
42/**
43 * Word Limiter
44 *
45 * Limits a string to X number of words.
46 *
47 * @access public
48 * @param string
49 * @param integer
50 * @param string the end character. Usually an ellipsis
51 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020052 */
Derek Allard2067d1a2008-11-13 22:59:24 +000053if ( ! function_exists('word_limiter'))
54{
55 function word_limiter($str, $limit = 100, $end_char = '&#8230;')
56 {
57 if (trim($str) == '')
58 {
59 return $str;
60 }
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064 if (strlen($str) == strlen($matches[0]))
65 {
66 $end_char = '';
67 }
Barry Mienydd671972010-10-04 16:33:58 +020068
Derek Allard2067d1a2008-11-13 22:59:24 +000069 return rtrim($matches[0]).$end_char;
70 }
71}
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073// ------------------------------------------------------------------------
74
75/**
76 * Character Limiter
77 *
Derek Jones37f4b9c2011-07-01 17:56:50 -050078 * Limits the string based on the character count. Preserves complete words
Derek Allard2067d1a2008-11-13 22:59:24 +000079 * so the character count may not be exactly as specified.
80 *
81 * @access public
82 * @param string
83 * @param integer
84 * @param string the end character. Usually an ellipsis
85 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020086 */
Derek Allard2067d1a2008-11-13 22:59:24 +000087if ( ! function_exists('character_limiter'))
88{
89 function character_limiter($str, $n = 500, $end_char = '&#8230;')
90 {
91 if (strlen($str) < $n)
92 {
93 return $str;
94 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Allard2067d1a2008-11-13 22:59:24 +000096 $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
97
98 if (strlen($str) <= $n)
99 {
100 return $str;
101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 $out = "";
104 foreach (explode(' ', trim($str)) as $val)
105 {
Derek Jones01d6b4f2009-02-03 14:51:00 +0000106 $out .= $val.' ';
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 if (strlen($out) >= $n)
109 {
Derek Jones01d6b4f2009-02-03 14:51:00 +0000110 $out = trim($out);
111 return (strlen($out) == strlen($str)) ? $out : $out.$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200112 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114 }
115}
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117// ------------------------------------------------------------------------
118
119/**
120 * High ASCII to Entities
121 *
122 * Converts High ascii text and MS Word special characters to character entities
123 *
124 * @access public
125 * @param string
126 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200127 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000128if ( ! function_exists('ascii_to_entities'))
129{
130 function ascii_to_entities($str)
131 {
Barry Mienydd671972010-10-04 16:33:58 +0200132 $count = 1;
133 $out = '';
134 $temp = array();
135
136 for ($i = 0, $s = strlen($str); $i < $s; $i++)
137 {
138 $ordinal = ord($str[$i]);
139
140 if ($ordinal < 128)
141 {
Derek Jones1978e122009-02-03 14:54:43 +0000142 /*
143 If the $temp array has a value but we have moved on, then it seems only
144 fair that we output that entity and restart $temp before continuing. -Paul
145 */
146 if (count($temp) == 1)
147 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500148 $out .= '&#'.array_shift($temp).';';
Derek Jones1978e122009-02-03 14:54:43 +0000149 $count = 1;
150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Jones1978e122009-02-03 14:54:43 +0000152 $out .= $str[$i];
Barry Mienydd671972010-10-04 16:33:58 +0200153 }
154 else
155 {
156 if (count($temp) == 0)
157 {
158 $count = ($ordinal < 224) ? 2 : 3;
159 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000160
Barry Mienydd671972010-10-04 16:33:58 +0200161 $temp[] = $ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000162
Barry Mienydd671972010-10-04 16:33:58 +0200163 if (count($temp) == $count)
164 {
165 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
166
167 $out .= '&#'.$number.';';
168 $count = 1;
169 $temp = array();
170 }
171 }
172 }
173
174 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
176}
Barry Mienydd671972010-10-04 16:33:58 +0200177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178// ------------------------------------------------------------------------
179
180/**
181 * Entities to ASCII
182 *
183 * Converts character entities back to ASCII
184 *
185 * @access public
186 * @param string
187 * @param bool
188 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200189 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000190if ( ! function_exists('entities_to_ascii'))
191{
192 function entities_to_ascii($str, $all = TRUE)
193 {
Barry Mienydd671972010-10-04 16:33:58 +0200194 if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
195 {
196 for ($i = 0, $s = count($matches['0']); $i < $s; $i++)
197 {
198 $digits = $matches['1'][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000199
Barry Mienydd671972010-10-04 16:33:58 +0200200 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000201
Barry Mienydd671972010-10-04 16:33:58 +0200202 if ($digits < 128)
203 {
204 $out .= chr($digits);
Derek Allard2067d1a2008-11-13 22:59:24 +0000205
Barry Mienydd671972010-10-04 16:33:58 +0200206 }
207 elseif ($digits < 2048)
208 {
209 $out .= chr(192 + (($digits - ($digits % 64)) / 64));
210 $out .= chr(128 + ($digits % 64));
211 }
212 else
213 {
214 $out .= chr(224 + (($digits - ($digits % 4096)) / 4096));
215 $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64));
216 $out .= chr(128 + ($digits % 64));
217 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000218
Barry Mienydd671972010-10-04 16:33:58 +0200219 $str = str_replace($matches['0'][$i], $out, $str);
220 }
221 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000222
Barry Mienydd671972010-10-04 16:33:58 +0200223 if ($all)
224 {
225 $str = str_replace(array("&amp;", "&lt;", "&gt;", "&quot;", "&apos;", "&#45;"),
226 array("&","<",">","\"", "'", "-"),
227 $str);
228 }
229
230 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
232}
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234// ------------------------------------------------------------------------
235
236/**
237 * Word Censoring Function
238 *
239 * Supply a string and an array of disallowed words and any
240 * matched words will be converted to #### or to the replacement
241 * word you've submitted.
242 *
243 * @access public
244 * @param string the text string
245 * @param string the array of censoered words
246 * @param string the optional replacement value
247 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200248 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000249if ( ! function_exists('word_censor'))
250{
251 function word_censor($str, $censored, $replacement = '')
252 {
253 if ( ! is_array($censored))
254 {
255 return $str;
256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
258 $str = ' '.$str.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000259
Derek Jonesf1b721a2009-01-21 17:52:13 +0000260 // \w, \b and a few others do not match on a unicode character
261 // set for performance reasons. As a result words like über
262 // will not match on a word boundary. Instead, we'll assume that
Derek Jonesdaf9c012010-03-05 10:29:30 -0600263 // a bad word will be bookeneded by any of these characters.
Derek Jonesf1b721a2009-01-21 17:52:13 +0000264 $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 foreach ($censored as $badword)
267 {
268 if ($replacement != '')
269 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000270 $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/i", "\\1{$replacement}\\3", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
272 else
273 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000274 $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/ie", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 }
276 }
Derek Jonesf1b721a2009-01-21 17:52:13 +0000277
Barry Mienydd671972010-10-04 16:33:58 +0200278 return trim($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
280}
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282// ------------------------------------------------------------------------
283
284/**
285 * Code Highlighter
286 *
287 * Colorizes code strings
288 *
289 * @access public
290 * @param string the text string
291 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200292 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000293if ( ! function_exists('highlight_code'))
294{
295 function highlight_code($str)
Barry Mienydd671972010-10-04 16:33:58 +0200296 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 // The highlight string function encodes and highlights
298 // brackets so we need them to start raw
299 $str = str_replace(array('&lt;', '&gt;'), array('<', '>'), $str);
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // Replace any existing PHP tags to temporary markers so they don't accidentally
302 // break the string out of PHP, and thus, thwart the highlighting.
Barry Mienydd671972010-10-04 16:33:58 +0200303
304 $str = str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'),
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str);
306
307 // The highlight_string function requires that the text be surrounded
308 // by PHP tags, which we will remove later
309 $str = '<?php '.$str.' ?>'; // <?
310
Barry Mienydd671972010-10-04 16:33:58 +0200311 // All the magic happens here, baby!
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 $str = highlight_string($str, TRUE);
313
314 // Prior to PHP 5, the highligh function used icky <font> tags
315 // so we'll replace them with <span> tags.
316
317 if (abs(PHP_VERSION) < 5)
318 {
319 $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
320 $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // Remove our artificially added PHP, and the syntax highlighting that came with it
324 $str = preg_replace('/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i', '<span style="color: #$1">', $str);
325 $str = preg_replace('/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is', "$1</span>\n</span>\n</code>", $str);
326 $str = preg_replace('/<span style="color: #[A-Z0-9]+"\><\/span>/i', '', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 // Replace our markers back to PHP tags.
329 $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
330 array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'), $str);
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 return $str;
333 }
334}
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336// ------------------------------------------------------------------------
337
338/**
339 * Phrase Highlighter
340 *
341 * Highlights a phrase within a text string
342 *
343 * @access public
344 * @param string the text string
345 * @param string the phrase you'd like to highlight
346 * @param string the openging tag to precede the phrase with
347 * @param string the closing tag to end the phrase with
348 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200349 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000350if ( ! function_exists('highlight_phrase'))
351{
352 function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
353 {
354 if ($str == '')
355 {
356 return '';
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 if ($phrase != '')
360 {
361 return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);
362 }
363
364 return $str;
365 }
366}
Derek Jonesdaf9c012010-03-05 10:29:30 -0600367
368// ------------------------------------------------------------------------
369
370/**
371 * Convert Accented Foreign Characters to ASCII
372 *
373 * @access public
374 * @param string the text string
375 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200376 */
Derek Jonesdaf9c012010-03-05 10:29:30 -0600377if ( ! function_exists('convert_accented_characters'))
378{
Eric Barnes5e044802011-01-11 16:10:26 -0500379 function convert_accented_characters($str)
Derek Jonesdaf9c012010-03-05 10:29:30 -0600380 {
Greg Aker3a746652011-04-19 10:59:47 -0500381 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
Derek Jonesdaf9c012010-03-05 10:29:30 -0600382 {
Greg Aker3a746652011-04-19 10:59:47 -0500383 include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
Derek Jonesdaf9c012010-03-05 10:29:30 -0600384 }
Greg Aker3a746652011-04-19 10:59:47 -0500385 elseif (is_file(APPPATH.'config/foreign_chars.php'))
bubbafoley0ea04142011-03-17 14:55:41 -0500386 {
Greg Aker3a746652011-04-19 10:59:47 -0500387 include(APPPATH.'config/foreign_chars.php');
bubbafoley0ea04142011-03-17 14:55:41 -0500388 }
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Jonesdaf9c012010-03-05 10:29:30 -0600390 if ( ! isset($foreign_characters))
391 {
Eric Barnes5e044802011-01-11 16:10:26 -0500392 return $str;
Derek Jonesdaf9c012010-03-05 10:29:30 -0600393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Eric Barnes5e044802011-01-11 16:10:26 -0500395 return preg_replace(array_keys($foreign_characters), array_values($foreign_characters), $str);
Derek Jonesdaf9c012010-03-05 10:29:30 -0600396 }
397}
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399// ------------------------------------------------------------------------
400
401/**
402 * Word Wrap
403 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500404 * Wraps text at the specified character. Maintains the integrity of words.
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
406 * will URLs.
407 *
408 * @access public
409 * @param string the text string
410 * @param integer the number of characters to wrap at
411 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200412 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000413if ( ! function_exists('word_wrap'))
414{
415 function word_wrap($str, $charlim = '76')
416 {
417 // Se the character limit
418 if ( ! is_numeric($charlim))
419 $charlim = 76;
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // Reduce multiple spaces
422 $str = preg_replace("| +|", " ", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 // Standardize newlines
425 if (strpos($str, "\r") !== FALSE)
426 {
Barry Mienydd671972010-10-04 16:33:58 +0200427 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
Barry Mienydd671972010-10-04 16:33:58 +0200429
430 // If the current word is surrounded by {unwrap} tags we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 // strip the entire chunk and replace it with a marker.
432 $unwrap = array();
433 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
434 {
435 for ($i = 0; $i < count($matches['0']); $i++)
436 {
Barry Mienydd671972010-10-04 16:33:58 +0200437 $unwrap[] = $matches['1'][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
439 }
440 }
Barry Mienydd671972010-10-04 16:33:58 +0200441
442 // Use PHP's native function to do the initial wordwrap.
443 // We set the cut flag to FALSE so that any individual words that are
Derek Jones37f4b9c2011-07-01 17:56:50 -0500444 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 $str = wordwrap($str, $charlim, "\n", FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 // Split the string into individual lines of text and cycle through them
448 $output = "";
Barry Mienydd671972010-10-04 16:33:58 +0200449 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
451 // Is the line within the allowed character count?
452 // If so we'll join it to the output and continue
453 if (strlen($line) <= $charlim)
454 {
Barry Mienydd671972010-10-04 16:33:58 +0200455 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 continue;
457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 $temp = '';
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500460 while ((strlen($line)) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
462 // If the over-length word is a URL we won't wrap it
463 if (preg_match("!\[url.+\]|://|wwww.!", $line))
464 {
465 break;
466 }
467
468 // Trim the word down
469 $temp .= substr($line, 0, $charlim-1);
470 $line = substr($line, $charlim-1);
471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
473 // If $temp contains data it means we had to split up an over-length
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 // word into smaller chunks so we'll add it back to our current line
475 if ($temp != '')
476 {
Barry Mienydd671972010-10-04 16:33:58 +0200477 $output .= $temp."\n".$line;
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 }
479 else
480 {
481 $output .= $line;
482 }
483
484 $output .= "\n";
485 }
486
487 // Put our markers back
488 if (count($unwrap) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200489 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 foreach ($unwrap as $key => $val)
491 {
492 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
493 }
494 }
495
496 // Remove the unwrap tags
497 $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
498
Barry Mienydd671972010-10-04 16:33:58 +0200499 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
501}
Barry Mienydd671972010-10-04 16:33:58 +0200502
Greg Akercbe32472010-08-05 14:09:20 -0500503// ------------------------------------------------------------------------
504
Greg Aker0f6b7c12010-08-05 14:11:14 -0500505/**
506 * Ellipsize String
507 *
508 * This function will strip tags from a string, split it at its max_length and ellipsize
509 *
Barry Mienydd671972010-10-04 16:33:58 +0200510 * @param string string to ellipsize
Greg Aker0f6b7c12010-08-05 14:11:14 -0500511 * @param integer max length of string
512 * @param mixed int (1|0) or float, .5, .2, etc for position to split
Barry Mienydd671972010-10-04 16:33:58 +0200513 * @param string ellipsis ; Default '...'
Greg Aker0f6b7c12010-08-05 14:11:14 -0500514 * @return string ellipsized string
515 */
Greg Akercbe32472010-08-05 14:09:20 -0500516if ( ! function_exists('ellipsize'))
517{
Greg Akercbe32472010-08-05 14:09:20 -0500518 function ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
519 {
520 // Strip tags
521 $str = trim(strip_tags($str));
522
523 // Is the string long enough to ellipsize?
524 if (strlen($str) <= $max_length)
525 {
526 return $str;
527 }
528
529 $beg = substr($str, 0, floor($max_length * $position));
530
531 $position = ($position > 1) ? 1 : $position;
532
533 if ($position === 1)
534 {
535 $end = substr($str, 0, -($max_length - strlen($beg)));
536 }
537 else
538 {
539 $end = substr($str, -($max_length - strlen($beg)));
540 }
541
Barry Mienydd671972010-10-04 16:33:58 +0200542 return $beg.$ellipsis.$end;
Greg Akercbe32472010-08-05 14:09:20 -0500543 }
544}
Derek Allard2067d1a2008-11-13 22:59:24 +0000545
546/* End of file text_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000547/* Location: ./system/helpers/text_helper.php */