blob: f53b06b93891ec2d3a1584a11f3626148db1296e [file] [log] [blame]
Andrey Andreeve684bda2012-03-26 13:47:29 +03001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreeve684bda2012-03-26 13:47:29 +03008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreeve684bda2012-03-26 13:47:29 +030010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter Text Helpers
30 *
31 * @package CodeIgniter
32 * @subpackage Helpers
33 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/helpers/text_helper.html
36 */
37
38// ------------------------------------------------------------------------
39
Derek Allard2067d1a2008-11-13 22:59:24 +000040if ( ! function_exists('word_limiter'))
41{
Timothy Warrenb75faa12012-04-27 12:03:32 -040042 /**
43 * Word Limiter
44 *
45 * Limits a string to X number of words.
46 *
47 * @param string
48 * @param int
49 * @param string the end character. Usually an ellipsis
50 * @return string
51 */
Derek Allard2067d1a2008-11-13 22:59:24 +000052 function word_limiter($str, $limit = 100, $end_char = '&#8230;')
53 {
54 if (trim($str) == '')
55 {
56 return $str;
57 }
Barry Mienydd671972010-10-04 16:33:58 +020058
Derek Allard2067d1a2008-11-13 22:59:24 +000059 preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 if (strlen($str) == strlen($matches[0]))
62 {
63 $end_char = '';
64 }
Barry Mienydd671972010-10-04 16:33:58 +020065
Derek Allard2067d1a2008-11-13 22:59:24 +000066 return rtrim($matches[0]).$end_char;
67 }
68}
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070// ------------------------------------------------------------------------
71
Derek Allard2067d1a2008-11-13 22:59:24 +000072if ( ! function_exists('character_limiter'))
73{
Timothy Warrenb75faa12012-04-27 12:03:32 -040074 /**
75 * Character Limiter
76 *
77 * Limits the string based on the character count. Preserves complete words
78 * so the character count may not be exactly as specified.
79 *
80 * @param string
81 * @param int
82 * @param string the end character. Usually an ellipsis
83 * @return string
84 */
Derek Allard2067d1a2008-11-13 22:59:24 +000085 function character_limiter($str, $n = 500, $end_char = '&#8230;')
86 {
87 if (strlen($str) < $n)
88 {
89 return $str;
90 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
93
94 if (strlen($str) <= $n)
95 {
96 return $str;
97 }
Barry Mienydd671972010-10-04 16:33:58 +020098
Derek Allard2067d1a2008-11-13 22:59:24 +000099 $out = "";
100 foreach (explode(' ', trim($str)) as $val)
101 {
Derek Jones01d6b4f2009-02-03 14:51:00 +0000102 $out .= $val.' ';
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 if (strlen($out) >= $n)
105 {
Derek Jones01d6b4f2009-02-03 14:51:00 +0000106 $out = trim($out);
107 return (strlen($out) == strlen($str)) ? $out : $out.$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200108 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
110 }
111}
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113// ------------------------------------------------------------------------
114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115if ( ! function_exists('ascii_to_entities'))
116{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400117 /**
118 * High ASCII to Entities
119 *
120 * Converts High ascii text and MS Word special characters to character entities
121 *
122 * @param string
123 * @return string
124 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 function ascii_to_entities($str)
126 {
Barry Mienydd671972010-10-04 16:33:58 +0200127 $count = 1;
128 $out = '';
129 $temp = array();
130
131 for ($i = 0, $s = strlen($str); $i < $s; $i++)
132 {
133 $ordinal = ord($str[$i]);
134
135 if ($ordinal < 128)
136 {
Derek Jones1978e122009-02-03 14:54:43 +0000137 /*
138 If the $temp array has a value but we have moved on, then it seems only
139 fair that we output that entity and restart $temp before continuing. -Paul
140 */
141 if (count($temp) == 1)
142 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500143 $out .= '&#'.array_shift($temp).';';
Derek Jones1978e122009-02-03 14:54:43 +0000144 $count = 1;
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Jones1978e122009-02-03 14:54:43 +0000147 $out .= $str[$i];
Barry Mienydd671972010-10-04 16:33:58 +0200148 }
149 else
150 {
151 if (count($temp) == 0)
152 {
153 $count = ($ordinal < 224) ? 2 : 3;
154 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000155
Barry Mienydd671972010-10-04 16:33:58 +0200156 $temp[] = $ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000157
Barry Mienydd671972010-10-04 16:33:58 +0200158 if (count($temp) == $count)
159 {
160 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
161
162 $out .= '&#'.$number.';';
163 $count = 1;
164 $temp = array();
165 }
166 }
167 }
168
169 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171}
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173// ------------------------------------------------------------------------
174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175if ( ! function_exists('entities_to_ascii'))
176{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400177 /**
178 * Entities to ASCII
179 *
180 * Converts character entities back to ASCII
181 *
182 * @param string
183 * @param bool
184 * @return string
185 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 function entities_to_ascii($str, $all = TRUE)
187 {
Barry Mienydd671972010-10-04 16:33:58 +0200188 if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
189 {
190 for ($i = 0, $s = count($matches['0']); $i < $s; $i++)
191 {
192 $digits = $matches['1'][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000193
Barry Mienydd671972010-10-04 16:33:58 +0200194 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000195
Barry Mienydd671972010-10-04 16:33:58 +0200196 if ($digits < 128)
197 {
198 $out .= chr($digits);
Derek Allard2067d1a2008-11-13 22:59:24 +0000199
Barry Mienydd671972010-10-04 16:33:58 +0200200 }
201 elseif ($digits < 2048)
202 {
203 $out .= chr(192 + (($digits - ($digits % 64)) / 64));
204 $out .= chr(128 + ($digits % 64));
205 }
206 else
207 {
208 $out .= chr(224 + (($digits - ($digits % 4096)) / 4096));
209 $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64));
210 $out .= chr(128 + ($digits % 64));
211 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000212
Barry Mienydd671972010-10-04 16:33:58 +0200213 $str = str_replace($matches['0'][$i], $out, $str);
214 }
215 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000216
Barry Mienydd671972010-10-04 16:33:58 +0200217 if ($all)
218 {
219 $str = str_replace(array("&amp;", "&lt;", "&gt;", "&quot;", "&apos;", "&#45;"),
220 array("&","<",">","\"", "'", "-"),
221 $str);
222 }
223
224 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
226}
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228// ------------------------------------------------------------------------
229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230if ( ! function_exists('word_censor'))
231{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400232 /**
233 * Word Censoring Function
234 *
235 * Supply a string and an array of disallowed words and any
236 * matched words will be converted to #### or to the replacement
237 * word you've submitted.
238 *
239 * @param string the text string
240 * @param string the array of censoered words
241 * @param string the optional replacement value
242 * @return string
243 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 function word_censor($str, $censored, $replacement = '')
245 {
246 if ( ! is_array($censored))
247 {
248 return $str;
249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
251 $str = ' '.$str.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000252
Derek Jonesf1b721a2009-01-21 17:52:13 +0000253 // \w, \b and a few others do not match on a unicode character
254 // set for performance reasons. As a result words like über
255 // will not match on a word boundary. Instead, we'll assume that
Derek Jonesdaf9c012010-03-05 10:29:30 -0600256 // a bad word will be bookeneded by any of these characters.
Derek Jonesf1b721a2009-01-21 17:52:13 +0000257 $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
258
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 foreach ($censored as $badword)
260 {
261 if ($replacement != '')
262 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000263 $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/i", "\\1{$replacement}\\3", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
265 else
266 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000267 $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 +0000268 }
269 }
Derek Jonesf1b721a2009-01-21 17:52:13 +0000270
Barry Mienydd671972010-10-04 16:33:58 +0200271 return trim($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
273}
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275// ------------------------------------------------------------------------
276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277if ( ! function_exists('highlight_code'))
278{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400279 /**
280 * Code Highlighter
281 *
282 * Colorizes code strings
283 *
284 * @param string the text string
285 * @return string
286 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 function highlight_code($str)
Barry Mienydd671972010-10-04 16:33:58 +0200288 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 // The highlight string function encodes and highlights
290 // brackets so we need them to start raw
291 $str = str_replace(array('&lt;', '&gt;'), array('<', '>'), $str);
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 // Replace any existing PHP tags to temporary markers so they don't accidentally
294 // break the string out of PHP, and thus, thwart the highlighting.
Barry Mienydd671972010-10-04 16:33:58 +0200295 $str = str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'),
Andrey Andreev8edf87d2012-03-01 20:20:03 +0200296 array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
297 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000298
299 // The highlight_string function requires that the text be surrounded
300 // by PHP tags, which we will remove later
301 $str = '<?php '.$str.' ?>'; // <?
302
Barry Mienydd671972010-10-04 16:33:58 +0200303 // All the magic happens here, baby!
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 $str = highlight_string($str, TRUE);
305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // Remove our artificially added PHP, and the syntax highlighting that came with it
307 $str = preg_replace('/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i', '<span style="color: #$1">', $str);
308 $str = preg_replace('/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is', "$1</span>\n</span>\n</code>", $str);
309 $str = preg_replace('/<span style="color: #[A-Z0-9]+"\><\/span>/i', '', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // Replace our markers back to PHP tags.
Andrey Andreev8edf87d2012-03-01 20:20:03 +0200312 return str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
313 array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'),
314 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
316}
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318// ------------------------------------------------------------------------
319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320if ( ! function_exists('highlight_phrase'))
321{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400322 /**
323 * Phrase Highlighter
324 *
325 * Highlights a phrase within a text string
326 *
327 * @param string the text string
328 * @param string the phrase you'd like to highlight
329 * @param string the openging tag to precede the phrase with
330 * @param string the closing tag to end the phrase with
331 * @return string
332 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
334 {
335 if ($str == '')
336 {
337 return '';
338 }
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 if ($phrase != '')
341 {
342 return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);
343 }
344
345 return $str;
346 }
347}
Derek Jonesdaf9c012010-03-05 10:29:30 -0600348
349// ------------------------------------------------------------------------
350
Derek Jonesdaf9c012010-03-05 10:29:30 -0600351if ( ! function_exists('convert_accented_characters'))
352{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400353 /**
354 * Convert Accented Foreign Characters to ASCII
355 *
356 * @param string the text string
357 * @return string
358 */
Eric Barnes5e044802011-01-11 16:10:26 -0500359 function convert_accented_characters($str)
Derek Jonesdaf9c012010-03-05 10:29:30 -0600360 {
Andrey Andreeve92df332012-03-26 22:44:20 +0300361 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600362 {
363 include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
364 }
365 elseif (is_file(APPPATH.'config/foreign_chars.php'))
366 {
367 include(APPPATH.'config/foreign_chars.php');
368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Jonesdaf9c012010-03-05 10:29:30 -0600370 if ( ! isset($foreign_characters))
371 {
Eric Barnes5e044802011-01-11 16:10:26 -0500372 return $str;
Derek Jonesdaf9c012010-03-05 10:29:30 -0600373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Eric Barnes5e044802011-01-11 16:10:26 -0500375 return preg_replace(array_keys($foreign_characters), array_values($foreign_characters), $str);
Derek Jonesdaf9c012010-03-05 10:29:30 -0600376 }
377}
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379// ------------------------------------------------------------------------
380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381if ( ! function_exists('word_wrap'))
382{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400383 /**
384 * Word Wrap
385 *
386 * Wraps text at the specified character. Maintains the integrity of words.
387 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
388 * will URLs.
389 *
390 * @param string the text string
391 * @param int the number of characters to wrap at
392 * @return string
393 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 function word_wrap($str, $charlim = '76')
395 {
396 // Se the character limit
397 if ( ! is_numeric($charlim))
398 $charlim = 76;
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 // Reduce multiple spaces
401 $str = preg_replace("| +|", " ", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 // Standardize newlines
404 if (strpos($str, "\r") !== FALSE)
405 {
Barry Mienydd671972010-10-04 16:33:58 +0200406 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
409 // If the current word is surrounded by {unwrap} tags we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 // strip the entire chunk and replace it with a marker.
411 $unwrap = array();
412 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
413 {
414 for ($i = 0; $i < count($matches['0']); $i++)
415 {
Barry Mienydd671972010-10-04 16:33:58 +0200416 $unwrap[] = $matches['1'][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
418 }
419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
421 // Use PHP's native function to do the initial wordwrap.
422 // We set the cut flag to FALSE so that any individual words that are
Derek Jones37f4b9c2011-07-01 17:56:50 -0500423 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 $str = wordwrap($str, $charlim, "\n", FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 // Split the string into individual lines of text and cycle through them
427 $output = "";
Barry Mienydd671972010-10-04 16:33:58 +0200428 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
430 // Is the line within the allowed character count?
431 // If so we'll join it to the output and continue
432 if (strlen($line) <= $charlim)
433 {
Barry Mienydd671972010-10-04 16:33:58 +0200434 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 continue;
436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 $temp = '';
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500439 while ((strlen($line)) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
441 // If the over-length word is a URL we won't wrap it
442 if (preg_match("!\[url.+\]|://|wwww.!", $line))
443 {
444 break;
445 }
446
447 // Trim the word down
448 $temp .= substr($line, 0, $charlim-1);
449 $line = substr($line, $charlim-1);
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
452 // If $temp contains data it means we had to split up an over-length
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 // word into smaller chunks so we'll add it back to our current line
454 if ($temp != '')
455 {
Barry Mienydd671972010-10-04 16:33:58 +0200456 $output .= $temp."\n".$line;
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
458 else
459 {
460 $output .= $line;
461 }
462
463 $output .= "\n";
464 }
465
466 // Put our markers back
467 if (count($unwrap) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200468 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 foreach ($unwrap as $key => $val)
470 {
471 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
472 }
473 }
474
475 // Remove the unwrap tags
476 $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
477
Barry Mienydd671972010-10-04 16:33:58 +0200478 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
480}
Barry Mienydd671972010-10-04 16:33:58 +0200481
Greg Akercbe32472010-08-05 14:09:20 -0500482// ------------------------------------------------------------------------
483
484if ( ! function_exists('ellipsize'))
485{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400486 /**
487 * Ellipsize String
488 *
489 * This function will strip tags from a string, split it at its max_length and ellipsize
490 *
491 * @param string string to ellipsize
492 * @param int max length of string
493 * @param mixed int (1|0) or float, .5, .2, etc for position to split
494 * @param string ellipsis ; Default '...'
495 * @return string ellipsized string
496 */
Greg Akercbe32472010-08-05 14:09:20 -0500497 function ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
498 {
499 // Strip tags
500 $str = trim(strip_tags($str));
501
502 // Is the string long enough to ellipsize?
503 if (strlen($str) <= $max_length)
504 {
505 return $str;
506 }
507
508 $beg = substr($str, 0, floor($max_length * $position));
509
510 $position = ($position > 1) ? 1 : $position;
511
512 if ($position === 1)
513 {
514 $end = substr($str, 0, -($max_length - strlen($beg)));
515 }
516 else
517 {
518 $end = substr($str, -($max_length - strlen($beg)));
519 }
520
Barry Mienydd671972010-10-04 16:33:58 +0200521 return $beg.$ellipsis.$end;
Greg Akercbe32472010-08-05 14:09:20 -0500522 }
523}
Derek Allard2067d1a2008-11-13 22:59:24 +0000524
525/* End of file text_helper.php */
Andrey Andreeve684bda2012-03-26 13:47:29 +0300526/* Location: ./system/helpers/text_helper.php */