blob: 8a1f01b513e248b557c6d5bfb05444a46cf4b51b [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
Andrey Andreevcb324bd2012-01-08 07:06:35 +020092 $str = preg_replace('/\s+/', ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
Derek Allard2067d1a2008-11-13 22:59:24 +000093
94 if (strlen($str) <= $n)
95 {
96 return $str;
97 }
Barry Mienydd671972010-10-04 16:33:58 +020098
Andrey Andreev4921fed2012-01-07 01:28:07 +020099 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 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);
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200107 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 */
Andrey Andreev4921fed2012-01-07 01:28:07 +0200141 if (count($temp) === 1)
Derek Jones1978e122009-02-03 14:54:43 +0000142 {
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 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200151 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200152 {
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
Andrey Andreev4921fed2012-01-07 01:28:07 +0200158 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200159 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200160 $number = ($count === 3)
161 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
162 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
Barry Mienydd671972010-10-04 16:33:58 +0200163
164 $out .= '&#'.$number.';';
165 $count = 1;
166 $temp = array();
167 }
168 }
169 }
170
171 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 }
173}
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175// ------------------------------------------------------------------------
176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177if ( ! function_exists('entities_to_ascii'))
178{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400179 /**
180 * Entities to ASCII
181 *
182 * Converts character entities back to ASCII
183 *
184 * @param string
185 * @param bool
186 * @return string
187 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 function entities_to_ascii($str, $all = TRUE)
189 {
Barry Mienydd671972010-10-04 16:33:58 +0200190 if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
191 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200192 for ($i = 0, $s = count($matches[0]); $i < $s; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200193 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200194 $digits = $matches[1][$i];
Barry Mienydd671972010-10-04 16:33:58 +0200195 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000196
Barry Mienydd671972010-10-04 16:33:58 +0200197 if ($digits < 128)
198 {
199 $out .= chr($digits);
Derek Allard2067d1a2008-11-13 22:59:24 +0000200
Barry Mienydd671972010-10-04 16:33:58 +0200201 }
202 elseif ($digits < 2048)
203 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200204 $out .= chr(192 + (($digits - ($digits % 64)) / 64)).chr(128 + ($digits % 64));
Barry Mienydd671972010-10-04 16:33:58 +0200205 }
206 else
207 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200208 $out .= chr(224 + (($digits - ($digits % 4096)) / 4096))
209 .chr(128 + ((($digits % 4096) - ($digits % 64)) / 64))
210 .chr(128 + ($digits % 64));
Barry Mienydd671972010-10-04 16:33:58 +0200211 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000212
Andrey Andreev4921fed2012-01-07 01:28:07 +0200213 $str = str_replace($matches[0][$i], $out, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200214 }
215 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000216
Barry Mienydd671972010-10-04 16:33:58 +0200217 if ($all)
218 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200219 return str_replace(array('&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#45;'),
220 array('&', '<', '>', '"', "'", '-'),
221 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200222 }
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 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100261 if ($replacement !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
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 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200289 /* The highlight string function encodes and highlights
290 * brackets so we need them to start raw.
291 *
292 * Also replace any existing PHP tags to temporary markers
293 * so they don't accidentally break the string out of PHP,
294 * and thus, thwart the highlighting.
295 */
296 $str = str_replace(array('&lt;', '&gt;', '<?', '?>', '<%', '%>', '\\', '</script>'),
297 array('<', '>', 'phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
Andrey Andreev8edf87d2012-03-01 20:20:03 +0200298 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000299
300 // The highlight_string function requires that the text be surrounded
301 // by PHP tags, which we will remove later
Andrey Andreev4921fed2012-01-07 01:28:07 +0200302 $str = highlight_string('<?php '.$str.' ?>', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 // Remove our artificially added PHP, and the syntax highlighting that came with it
Andrey Andreev4921fed2012-01-07 01:28:07 +0200305 $str = preg_replace(array(
306 '/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i',
307 '/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is',
308 '/<span style="color: #[A-Z0-9]+"\><\/span>/i'
309 ),
310 array(
311 '<span style="color: #$1">',
312 "$1</span>\n</span>\n</code>",
313 ''
314 ),
315 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // Replace our markers back to PHP tags.
Andrey Andreev8edf87d2012-03-01 20:20:03 +0200318 return str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
319 array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'),
320 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
322}
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324// ------------------------------------------------------------------------
325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326if ( ! function_exists('highlight_phrase'))
327{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400328 /**
329 * Phrase Highlighter
330 *
331 * Highlights a phrase within a text string
332 *
333 * @param string the text string
334 * @param string the phrase you'd like to highlight
335 * @param string the openging tag to precede the phrase with
336 * @param string the closing tag to end the phrase with
337 * @return string
338 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
340 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100341 if ($str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
343 return '';
344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Alex Bilbie773ccc32012-06-02 11:11:08 +0100346 if ($phrase !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200348 return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open.'\\1'.$tag_close, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 }
350
351 return $str;
352 }
353}
Derek Jonesdaf9c012010-03-05 10:29:30 -0600354
355// ------------------------------------------------------------------------
356
Derek Jonesdaf9c012010-03-05 10:29:30 -0600357if ( ! function_exists('convert_accented_characters'))
358{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400359 /**
360 * Convert Accented Foreign Characters to ASCII
361 *
362 * @param string the text string
363 * @return string
364 */
Eric Barnes5e044802011-01-11 16:10:26 -0500365 function convert_accented_characters($str)
Derek Jonesdaf9c012010-03-05 10:29:30 -0600366 {
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200367 global $foreign_characters;
Barry Mienydd671972010-10-04 16:33:58 +0200368
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200369 if ( ! isset($foreign_characters) OR ! is_array($foreign_characters))
Derek Jonesdaf9c012010-03-05 10:29:30 -0600370 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200371 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200372 {
373 include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
374 }
375 elseif (is_file(APPPATH.'config/foreign_chars.php'))
376 {
377 include(APPPATH.'config/foreign_chars.php');
378 }
379
Andrey Andreev963c96c2012-05-02 13:09:57 +0300380 if ( ! isset($foreign_characters) OR ! is_array($foreign_characters))
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200381 {
382 return $str;
383 }
Derek Jonesdaf9c012010-03-05 10:29:30 -0600384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Eric Barnes5e044802011-01-11 16:10:26 -0500386 return preg_replace(array_keys($foreign_characters), array_values($foreign_characters), $str);
Derek Jonesdaf9c012010-03-05 10:29:30 -0600387 }
388}
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390// ------------------------------------------------------------------------
391
392/**
393 * Word Wrap
394 *
Andrey Andreev09375d72012-01-19 14:57:46 +0200395 * Wraps text at the specified character. Maintains the integrity of words.
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
397 * will URLs.
398 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 * @param string the text string
Andrey Andreev75124a52012-03-13 12:47:58 +0200400 * @param int the number of characters to wrap at
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200402 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000403if ( ! function_exists('word_wrap'))
404{
Andrey Andreevfc443552012-01-07 02:14:55 +0200405 function word_wrap($str, $charlim = 76)
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200407 // Set the character limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 if ( ! is_numeric($charlim))
Andrey Andreev4921fed2012-01-07 01:28:07 +0200409 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 $charlim = 76;
Andrey Andreev4921fed2012-01-07 01:28:07 +0200411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 // Reduce multiple spaces
Andrey Andreev4921fed2012-01-07 01:28:07 +0200414 $str = preg_replace('| +|', ' ', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 // Standardize newlines
417 if (strpos($str, "\r") !== FALSE)
418 {
Barry Mienydd671972010-10-04 16:33:58 +0200419 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
422 // If the current word is surrounded by {unwrap} tags we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 // strip the entire chunk and replace it with a marker.
424 $unwrap = array();
Andrey Andreev4921fed2012-01-07 01:28:07 +0200425 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200427 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200429 $unwrap[] = $matches[1][$i];
430 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
434 // Use PHP's native function to do the initial wordwrap.
435 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev4921fed2012-01-07 01:28:07 +0200436 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 $str = wordwrap($str, $charlim, "\n", FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 // Split the string into individual lines of text and cycle through them
Andrey Andreev4921fed2012-01-07 01:28:07 +0200440 $output = '';
Barry Mienydd671972010-10-04 16:33:58 +0200441 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
443 // Is the line within the allowed character count?
444 // If so we'll join it to the output and continue
445 if (strlen($line) <= $charlim)
446 {
Barry Mienydd671972010-10-04 16:33:58 +0200447 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 continue;
449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 $temp = '';
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500452 while ((strlen($line)) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
454 // If the over-length word is a URL we won't wrap it
Andrey Andreev4921fed2012-01-07 01:28:07 +0200455 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
457 break;
458 }
459
460 // Trim the word down
Andrey Andreev75124a52012-03-13 12:47:58 +0200461 $temp .= substr($line, 0, $charlim - 1);
462 $line = substr($line, $charlim - 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
465 // If $temp contains data it means we had to split up an over-length
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 // word into smaller chunks so we'll add it back to our current line
Alex Bilbie773ccc32012-06-02 11:11:08 +0100467 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200469 $output .= $temp."\n".$line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 }
471 else
472 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200473 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
476
477 // Put our markers back
478 if (count($unwrap) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200479 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 foreach ($unwrap as $key => $val)
481 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200482 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 }
484 }
485
Andrey Andreev4921fed2012-01-07 01:28:07 +0200486 // Remove the unwrap tags and return
487 return str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
489}
Barry Mienydd671972010-10-04 16:33:58 +0200490
Greg Akercbe32472010-08-05 14:09:20 -0500491// ------------------------------------------------------------------------
492
493if ( ! function_exists('ellipsize'))
494{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400495 /**
496 * Ellipsize String
497 *
498 * This function will strip tags from a string, split it at its max_length and ellipsize
499 *
500 * @param string string to ellipsize
501 * @param int max length of string
502 * @param mixed int (1|0) or float, .5, .2, etc for position to split
503 * @param string ellipsis ; Default '...'
504 * @return string ellipsized string
505 */
Greg Akercbe32472010-08-05 14:09:20 -0500506 function ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
507 {
508 // Strip tags
509 $str = trim(strip_tags($str));
510
511 // Is the string long enough to ellipsize?
512 if (strlen($str) <= $max_length)
513 {
514 return $str;
515 }
516
517 $beg = substr($str, 0, floor($max_length * $position));
Greg Akercbe32472010-08-05 14:09:20 -0500518 $position = ($position > 1) ? 1 : $position;
519
520 if ($position === 1)
521 {
522 $end = substr($str, 0, -($max_length - strlen($beg)));
523 }
524 else
525 {
526 $end = substr($str, -($max_length - strlen($beg)));
527 }
528
Barry Mienydd671972010-10-04 16:33:58 +0200529 return $beg.$ellipsis.$end;
Greg Akercbe32472010-08-05 14:09:20 -0500530 }
531}
Derek Allard2067d1a2008-11-13 22:59:24 +0000532
533/* End of file text_helper.php */
Andrey Andreeve684bda2012-03-26 13:47:29 +0300534/* Location: ./system/helpers/text_helper.php */