blob: 89602fc287360340ee315579e0d6f603ba87039c [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 {
Alex Bilbie773ccc32012-06-02 11:11:08 +010054 if (trim($str) === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000055 {
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
Andrey Andreev4921fed2012-01-07 01:28:07 +020061 if (strlen($str) === strlen($matches[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +000062 {
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
vlakoff62a5ee32012-09-04 23:12:49 +020092 // a bit complicated, but faster than preg_replace with \s+
93 $str = preg_replace('/ {2,}/', ' ', str_replace(array("\r", "\n", "\t", "\x0B", "\x0C"), ' ', $str));
Derek Allard2067d1a2008-11-13 22:59:24 +000094
95 if (strlen($str) <= $n)
96 {
97 return $str;
98 }
Barry Mienydd671972010-10-04 16:33:58 +020099
Andrey Andreev4921fed2012-01-07 01:28:07 +0200100 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 foreach (explode(' ', trim($str)) as $val)
102 {
Derek Jones01d6b4f2009-02-03 14:51:00 +0000103 $out .= $val.' ';
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 if (strlen($out) >= $n)
106 {
Derek Jones01d6b4f2009-02-03 14:51:00 +0000107 $out = trim($out);
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200108 return (strlen($out) === strlen($str)) ? $out : $out.$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200109 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 }
111 }
112}
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114// ------------------------------------------------------------------------
115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116if ( ! function_exists('ascii_to_entities'))
117{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400118 /**
119 * High ASCII to Entities
120 *
Andrey Andreev7d753462012-10-27 03:37:40 +0300121 * Converts high ASCII text and MS Word special characters to character entities
Timothy Warrenb75faa12012-04-27 12:03:32 -0400122 *
Andrey Andreev7d753462012-10-27 03:37:40 +0300123 * @param string $str
Timothy Warrenb75faa12012-04-27 12:03:32 -0400124 * @return string
125 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 function ascii_to_entities($str)
127 {
Andrey Andreev7d753462012-10-27 03:37:40 +0300128 $out = '';
129 for ($i = 0, $s = strlen($str), $count = 1, $temp = array(); $i < $s; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200130 {
131 $ordinal = ord($str[$i]);
132
133 if ($ordinal < 128)
134 {
Derek Jones1978e122009-02-03 14:54:43 +0000135 /*
136 If the $temp array has a value but we have moved on, then it seems only
137 fair that we output that entity and restart $temp before continuing. -Paul
138 */
Andrey Andreev4921fed2012-01-07 01:28:07 +0200139 if (count($temp) === 1)
Derek Jones1978e122009-02-03 14:54:43 +0000140 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500141 $out .= '&#'.array_shift($temp).';';
Derek Jones1978e122009-02-03 14:54:43 +0000142 $count = 1;
143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Jones1978e122009-02-03 14:54:43 +0000145 $out .= $str[$i];
Barry Mienydd671972010-10-04 16:33:58 +0200146 }
147 else
148 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200149 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200150 {
151 $count = ($ordinal < 224) ? 2 : 3;
152 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000153
Barry Mienydd671972010-10-04 16:33:58 +0200154 $temp[] = $ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000155
Andrey Andreev4921fed2012-01-07 01:28:07 +0200156 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200157 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200158 $number = ($count === 3)
159 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
160 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
Barry Mienydd671972010-10-04 16:33:58 +0200161
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 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200190 for ($i = 0, $s = count($matches[0]); $i < $s; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200191 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200192 $digits = $matches[1][$i];
Barry Mienydd671972010-10-04 16:33:58 +0200193 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000194
Barry Mienydd671972010-10-04 16:33:58 +0200195 if ($digits < 128)
196 {
197 $out .= chr($digits);
Derek Allard2067d1a2008-11-13 22:59:24 +0000198
Barry Mienydd671972010-10-04 16:33:58 +0200199 }
200 elseif ($digits < 2048)
201 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200202 $out .= chr(192 + (($digits - ($digits % 64)) / 64)).chr(128 + ($digits % 64));
Barry Mienydd671972010-10-04 16:33:58 +0200203 }
204 else
205 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200206 $out .= chr(224 + (($digits - ($digits % 4096)) / 4096))
207 .chr(128 + ((($digits % 4096) - ($digits % 64)) / 64))
208 .chr(128 + ($digits % 64));
Barry Mienydd671972010-10-04 16:33:58 +0200209 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000210
Andrey Andreev4921fed2012-01-07 01:28:07 +0200211 $str = str_replace($matches[0][$i], $out, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200212 }
213 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000214
Barry Mienydd671972010-10-04 16:33:58 +0200215 if ($all)
216 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200217 return str_replace(array('&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#45;'),
218 array('&', '<', '>', '"', "'", '-'),
219 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200220 }
221
222 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
224}
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226// ------------------------------------------------------------------------
227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228if ( ! function_exists('word_censor'))
229{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400230 /**
231 * Word Censoring Function
232 *
233 * Supply a string and an array of disallowed words and any
234 * matched words will be converted to #### or to the replacement
235 * word you've submitted.
236 *
237 * @param string the text string
238 * @param string the array of censoered words
239 * @param string the optional replacement value
240 * @return string
241 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 function word_censor($str, $censored, $replacement = '')
243 {
244 if ( ! is_array($censored))
245 {
246 return $str;
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
249 $str = ' '.$str.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000250
Derek Jonesf1b721a2009-01-21 17:52:13 +0000251 // \w, \b and a few others do not match on a unicode character
252 // set for performance reasons. As a result words like über
253 // will not match on a word boundary. Instead, we'll assume that
Derek Jonesdaf9c012010-03-05 10:29:30 -0600254 // a bad word will be bookeneded by any of these characters.
Derek Jonesf1b721a2009-01-21 17:52:13 +0000255 $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 foreach ($censored as $badword)
258 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100259 if ($replacement !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000261 $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/i", "\\1{$replacement}\\3", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 }
263 else
264 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000265 $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 +0000266 }
267 }
Derek Jonesf1b721a2009-01-21 17:52:13 +0000268
Barry Mienydd671972010-10-04 16:33:58 +0200269 return trim($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
271}
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273// ------------------------------------------------------------------------
274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275if ( ! function_exists('highlight_code'))
276{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400277 /**
278 * Code Highlighter
279 *
280 * Colorizes code strings
281 *
282 * @param string the text string
283 * @return string
284 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 function highlight_code($str)
Barry Mienydd671972010-10-04 16:33:58 +0200286 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200287 /* The highlight string function encodes and highlights
288 * brackets so we need them to start raw.
289 *
290 * Also replace any existing PHP tags to temporary markers
291 * so they don't accidentally break the string out of PHP,
292 * and thus, thwart the highlighting.
293 */
294 $str = str_replace(array('&lt;', '&gt;', '<?', '?>', '<%', '%>', '\\', '</script>'),
295 array('<', '>', 'phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
Andrey Andreev8edf87d2012-03-01 20:20:03 +0200296 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000297
298 // The highlight_string function requires that the text be surrounded
299 // by PHP tags, which we will remove later
Andrey Andreev4921fed2012-01-07 01:28:07 +0200300 $str = highlight_string('<?php '.$str.' ?>', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 // Remove our artificially added PHP, and the syntax highlighting that came with it
Andrey Andreev4921fed2012-01-07 01:28:07 +0200303 $str = preg_replace(array(
304 '/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i',
305 '/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is',
306 '/<span style="color: #[A-Z0-9]+"\><\/span>/i'
307 ),
308 array(
309 '<span style="color: #$1">',
310 "$1</span>\n</span>\n</code>",
311 ''
312 ),
313 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 // Replace our markers back to PHP tags.
Andrey Andreev8edf87d2012-03-01 20:20:03 +0200316 return str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
317 array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'),
318 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 }
320}
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322// ------------------------------------------------------------------------
323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324if ( ! function_exists('highlight_phrase'))
325{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400326 /**
327 * Phrase Highlighter
328 *
329 * Highlights a phrase within a text string
330 *
331 * @param string the text string
332 * @param string the phrase you'd like to highlight
333 * @param string the openging tag to precede the phrase with
334 * @param string the closing tag to end the phrase with
335 * @return string
336 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
338 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100339 if ($str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
341 return '';
342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Alex Bilbie773ccc32012-06-02 11:11:08 +0100344 if ($phrase !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200346 return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open.'\\1'.$tag_close, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
348
349 return $str;
350 }
351}
Derek Jonesdaf9c012010-03-05 10:29:30 -0600352
353// ------------------------------------------------------------------------
354
Derek Jonesdaf9c012010-03-05 10:29:30 -0600355if ( ! function_exists('convert_accented_characters'))
356{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400357 /**
358 * Convert Accented Foreign Characters to ASCII
359 *
360 * @param string the text string
361 * @return string
362 */
Eric Barnes5e044802011-01-11 16:10:26 -0500363 function convert_accented_characters($str)
Derek Jonesdaf9c012010-03-05 10:29:30 -0600364 {
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200365 global $foreign_characters;
Barry Mienydd671972010-10-04 16:33:58 +0200366
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200367 if ( ! isset($foreign_characters) OR ! is_array($foreign_characters))
Derek Jonesdaf9c012010-03-05 10:29:30 -0600368 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200369 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200370 {
371 include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
372 }
373 elseif (is_file(APPPATH.'config/foreign_chars.php'))
374 {
375 include(APPPATH.'config/foreign_chars.php');
376 }
377
Andrey Andreev963c96c2012-05-02 13:09:57 +0300378 if ( ! isset($foreign_characters) OR ! is_array($foreign_characters))
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200379 {
380 return $str;
381 }
Derek Jonesdaf9c012010-03-05 10:29:30 -0600382 }
Barry Mienydd671972010-10-04 16:33:58 +0200383
Eric Barnes5e044802011-01-11 16:10:26 -0500384 return preg_replace(array_keys($foreign_characters), array_values($foreign_characters), $str);
Derek Jonesdaf9c012010-03-05 10:29:30 -0600385 }
386}
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388// ------------------------------------------------------------------------
389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390if ( ! function_exists('word_wrap'))
391{
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300392 /**
393 * Word Wrap
394 *
395 * Wraps text at the specified character. Maintains the integrity of words.
396 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
397 * will URLs.
398 *
399 * @param string $str the text string
400 * @param int $charlim = 76 the number of characters to wrap at
401 * @return string
402 */
Andrey Andreevfc443552012-01-07 02:14:55 +0200403 function word_wrap($str, $charlim = 76)
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200405 // Set the character limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 if ( ! is_numeric($charlim))
Andrey Andreev4921fed2012-01-07 01:28:07 +0200407 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 $charlim = 76;
Andrey Andreev4921fed2012-01-07 01:28:07 +0200409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // Reduce multiple spaces
Andrey Andreev4921fed2012-01-07 01:28:07 +0200412 $str = preg_replace('| +|', ' ', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 // Standardize newlines
415 if (strpos($str, "\r") !== FALSE)
416 {
Barry Mienydd671972010-10-04 16:33:58 +0200417 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
420 // If the current word is surrounded by {unwrap} tags we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // strip the entire chunk and replace it with a marker.
422 $unwrap = array();
Andrey Andreev4921fed2012-01-07 01:28:07 +0200423 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200425 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200427 $unwrap[] = $matches[1][$i];
428 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
432 // Use PHP's native function to do the initial wordwrap.
433 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev4921fed2012-01-07 01:28:07 +0200434 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 $str = wordwrap($str, $charlim, "\n", FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 // Split the string into individual lines of text and cycle through them
Andrey Andreev4921fed2012-01-07 01:28:07 +0200438 $output = '';
Barry Mienydd671972010-10-04 16:33:58 +0200439 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
441 // Is the line within the allowed character count?
442 // If so we'll join it to the output and continue
443 if (strlen($line) <= $charlim)
444 {
Barry Mienydd671972010-10-04 16:33:58 +0200445 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 continue;
447 }
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 $temp = '';
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500450 while ((strlen($line)) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
452 // If the over-length word is a URL we won't wrap it
Andrey Andreev4921fed2012-01-07 01:28:07 +0200453 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
455 break;
456 }
457
458 // Trim the word down
Andrey Andreev75124a52012-03-13 12:47:58 +0200459 $temp .= substr($line, 0, $charlim - 1);
460 $line = substr($line, $charlim - 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
463 // If $temp contains data it means we had to split up an over-length
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 // word into smaller chunks so we'll add it back to our current line
Alex Bilbie773ccc32012-06-02 11:11:08 +0100465 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200467 $output .= $temp."\n".$line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
469 else
470 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200471 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
474
475 // Put our markers back
476 if (count($unwrap) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200477 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 foreach ($unwrap as $key => $val)
479 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200480 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
482 }
483
Andrey Andreev4921fed2012-01-07 01:28:07 +0200484 // Remove the unwrap tags and return
485 return str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 }
487}
Barry Mienydd671972010-10-04 16:33:58 +0200488
Greg Akercbe32472010-08-05 14:09:20 -0500489// ------------------------------------------------------------------------
490
491if ( ! function_exists('ellipsize'))
492{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400493 /**
494 * Ellipsize String
495 *
496 * This function will strip tags from a string, split it at its max_length and ellipsize
497 *
498 * @param string string to ellipsize
499 * @param int max length of string
500 * @param mixed int (1|0) or float, .5, .2, etc for position to split
501 * @param string ellipsis ; Default '...'
502 * @return string ellipsized string
503 */
Greg Akercbe32472010-08-05 14:09:20 -0500504 function ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
505 {
506 // Strip tags
507 $str = trim(strip_tags($str));
508
509 // Is the string long enough to ellipsize?
510 if (strlen($str) <= $max_length)
511 {
512 return $str;
513 }
514
515 $beg = substr($str, 0, floor($max_length * $position));
Greg Akercbe32472010-08-05 14:09:20 -0500516 $position = ($position > 1) ? 1 : $position;
517
518 if ($position === 1)
519 {
520 $end = substr($str, 0, -($max_length - strlen($beg)));
521 }
522 else
523 {
524 $end = substr($str, -($max_length - strlen($beg)));
525 }
526
Barry Mienydd671972010-10-04 16:33:58 +0200527 return $beg.$ellipsis.$end;
Greg Akercbe32472010-08-05 14:09:20 -0500528 }
529}
Derek Allard2067d1a2008-11-13 22:59:24 +0000530
531/* End of file text_helper.php */
Andrey Andreeve684bda2012-03-26 13:47:29 +0300532/* Location: ./system/helpers/text_helper.php */