blob: 37b5d3178b74e93482d9c6e8cdcdd69205bf1104 [file] [log] [blame]
Andrey Andreev4921fed2012-01-07 01:28:07 +02001<?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
Andrey Andreev4921fed2012-01-07 01:28:07 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4921fed2012-01-07 01:28:07 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
Andrey Andreevc5a1f932012-01-24 15:29:02 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * 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
40/**
41 * Word Limiter
42 *
43 * Limits a string to X number of words.
44 *
45 * @access public
46 * @param string
47 * @param integer
48 * @param string the end character. Usually an ellipsis
49 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020050 */
Derek Allard2067d1a2008-11-13 22:59:24 +000051if ( ! function_exists('word_limiter'))
52{
53 function word_limiter($str, $limit = 100, $end_char = '&#8230;')
54 {
55 if (trim($str) == '')
56 {
57 return $str;
58 }
Barry Mienydd671972010-10-04 16:33:58 +020059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
Barry Mienydd671972010-10-04 16:33:58 +020061
Andrey Andreev4921fed2012-01-07 01:28:07 +020062 if (strlen($str) === strlen($matches[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +000063 {
64 $end_char = '';
65 }
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Allard2067d1a2008-11-13 22:59:24 +000067 return rtrim($matches[0]).$end_char;
68 }
69}
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071// ------------------------------------------------------------------------
72
73/**
74 * Character Limiter
75 *
Andrey Andreev09375d72012-01-19 14:57:46 +020076 * Limits the string based on the character count. Preserves complete words
Derek Allard2067d1a2008-11-13 22:59:24 +000077 * so the character count may not be exactly as specified.
78 *
79 * @access public
80 * @param string
81 * @param integer
82 * @param string the end character. Usually an ellipsis
83 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020084 */
Derek Allard2067d1a2008-11-13 22:59:24 +000085if ( ! function_exists('character_limiter'))
86{
87 function character_limiter($str, $n = 500, $end_char = '&#8230;')
88 {
89 if (strlen($str) < $n)
90 {
91 return $str;
92 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Andrey Andreevcb324bd2012-01-08 07:06:35 +020094 $str = preg_replace('/\s+/', ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
Derek Allard2067d1a2008-11-13 22:59:24 +000095
96 if (strlen($str) <= $n)
97 {
98 return $str;
99 }
Barry Mienydd671972010-10-04 16:33:58 +0200100
Andrey Andreev4921fed2012-01-07 01:28:07 +0200101 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 foreach (explode(' ', trim($str)) as $val)
103 {
Derek Jones01d6b4f2009-02-03 14:51:00 +0000104 $out .= $val.' ';
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 if (strlen($out) >= $n)
107 {
Derek Jones01d6b4f2009-02-03 14:51:00 +0000108 $out = trim($out);
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200109 return (strlen($out) === strlen($str)) ? $out : $out.$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200110 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112 }
113}
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115// ------------------------------------------------------------------------
116
117/**
118 * High ASCII to Entities
119 *
120 * Converts High ascii text and MS Word special characters to character entities
121 *
122 * @access public
123 * @param string
124 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200125 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000126if ( ! function_exists('ascii_to_entities'))
127{
128 function ascii_to_entities($str)
129 {
Barry Mienydd671972010-10-04 16:33:58 +0200130 $count = 1;
131 $out = '';
132 $temp = array();
133
134 for ($i = 0, $s = strlen($str); $i < $s; $i++)
135 {
136 $ordinal = ord($str[$i]);
137
138 if ($ordinal < 128)
139 {
Derek Jones1978e122009-02-03 14:54:43 +0000140 /*
141 If the $temp array has a value but we have moved on, then it seems only
142 fair that we output that entity and restart $temp before continuing. -Paul
143 */
Andrey Andreev4921fed2012-01-07 01:28:07 +0200144 if (count($temp) === 1)
Derek Jones1978e122009-02-03 14:54:43 +0000145 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500146 $out .= '&#'.array_shift($temp).';';
Derek Jones1978e122009-02-03 14:54:43 +0000147 $count = 1;
148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Jones1978e122009-02-03 14:54:43 +0000150 $out .= $str[$i];
Barry Mienydd671972010-10-04 16:33:58 +0200151 }
152 else
153 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200154 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200155 {
156 $count = ($ordinal < 224) ? 2 : 3;
157 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000158
Barry Mienydd671972010-10-04 16:33:58 +0200159 $temp[] = $ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000160
Andrey Andreev4921fed2012-01-07 01:28:07 +0200161 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200162 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200163 $number = ($count === 3)
164 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
165 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
Barry Mienydd671972010-10-04 16:33:58 +0200166
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 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200196 for ($i = 0, $s = count($matches[0]); $i < $s; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200197 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200198 $digits = $matches[1][$i];
Barry Mienydd671972010-10-04 16:33:58 +0200199 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000200
Barry Mienydd671972010-10-04 16:33:58 +0200201 if ($digits < 128)
202 {
203 $out .= chr($digits);
Derek Allard2067d1a2008-11-13 22:59:24 +0000204
Barry Mienydd671972010-10-04 16:33:58 +0200205 }
206 elseif ($digits < 2048)
207 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200208 $out .= chr(192 + (($digits - ($digits % 64)) / 64)).chr(128 + ($digits % 64));
Barry Mienydd671972010-10-04 16:33:58 +0200209 }
210 else
211 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200212 $out .= chr(224 + (($digits - ($digits % 4096)) / 4096))
213 .chr(128 + ((($digits % 4096) - ($digits % 64)) / 64))
214 .chr(128 + ($digits % 64));
Barry Mienydd671972010-10-04 16:33:58 +0200215 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000216
Andrey Andreev4921fed2012-01-07 01:28:07 +0200217 $str = str_replace($matches[0][$i], $out, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200218 }
219 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000220
Barry Mienydd671972010-10-04 16:33:58 +0200221 if ($all)
222 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200223 return str_replace(array('&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#45;'),
224 array('&', '<', '>', '"', "'", '-'),
225 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200226 }
227
228 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
230}
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232// ------------------------------------------------------------------------
233
234/**
235 * Word Censoring Function
236 *
237 * Supply a string and an array of disallowed words and any
238 * matched words will be converted to #### or to the replacement
239 * word you've submitted.
240 *
241 * @access public
242 * @param string the text string
243 * @param string the array of censoered words
244 * @param string the optional replacement value
245 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200246 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000247if ( ! function_exists('word_censor'))
248{
249 function word_censor($str, $censored, $replacement = '')
250 {
251 if ( ! is_array($censored))
252 {
253 return $str;
254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
256 $str = ' '.$str.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000257
Derek Jonesf1b721a2009-01-21 17:52:13 +0000258 // \w, \b and a few others do not match on a unicode character
259 // set for performance reasons. As a result words like über
260 // will not match on a word boundary. Instead, we'll assume that
Derek Jonesdaf9c012010-03-05 10:29:30 -0600261 // a bad word will be bookeneded by any of these characters.
Derek Jonesf1b721a2009-01-21 17:52:13 +0000262 $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 foreach ($censored as $badword)
265 {
266 if ($replacement != '')
267 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000268 $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/i", "\\1{$replacement}\\3", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 }
270 else
271 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000272 $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 +0000273 }
274 }
Derek Jonesf1b721a2009-01-21 17:52:13 +0000275
Barry Mienydd671972010-10-04 16:33:58 +0200276 return trim($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 }
278}
Barry Mienydd671972010-10-04 16:33:58 +0200279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280// ------------------------------------------------------------------------
281
282/**
283 * Code Highlighter
284 *
285 * Colorizes code strings
286 *
287 * @access public
288 * @param string the text string
289 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200290 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000291if ( ! function_exists('highlight_code'))
292{
293 function highlight_code($str)
Barry Mienydd671972010-10-04 16:33:58 +0200294 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200295 /* The highlight string function encodes and highlights
296 * brackets so we need them to start raw.
297 *
298 * Also replace any existing PHP tags to temporary markers
299 * so they don't accidentally break the string out of PHP,
300 * and thus, thwart the highlighting.
301 */
302 $str = str_replace(array('&lt;', '&gt;', '<?', '?>', '<%', '%>', '\\', '</script>'),
303 array('<', '>', 'phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
304 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000305
306 // The highlight_string function requires that the text be surrounded
307 // by PHP tags, which we will remove later
Andrey Andreev4921fed2012-01-07 01:28:07 +0200308 $str = highlight_string('<?php '.$str.' ?>', TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 // Remove our artificially added PHP, and the syntax highlighting that came with it
Andrey Andreev4921fed2012-01-07 01:28:07 +0200311 $str = preg_replace(array(
312 '/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i',
313 '/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is',
314 '/<span style="color: #[A-Z0-9]+"\><\/span>/i'
315 ),
316 array(
317 '<span style="color: #$1">',
318 "$1</span>\n</span>\n</code>",
319 ''
320 ),
321 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // Replace our markers back to PHP tags.
Andrey Andreev4921fed2012-01-07 01:28:07 +0200324 return str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
325 array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'),
326 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
328}
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330// ------------------------------------------------------------------------
331
332/**
333 * Phrase Highlighter
334 *
335 * Highlights a phrase within a text string
336 *
337 * @access public
338 * @param string the text string
339 * @param string the phrase you'd like to highlight
340 * @param string the openging tag to precede the phrase with
341 * @param string the closing tag to end the phrase with
342 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200343 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000344if ( ! function_exists('highlight_phrase'))
345{
346 function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
347 {
348 if ($str == '')
349 {
350 return '';
351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 if ($phrase != '')
354 {
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200355 return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open.'\\1'.$tag_close, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
357
358 return $str;
359 }
360}
Derek Jonesdaf9c012010-03-05 10:29:30 -0600361
362// ------------------------------------------------------------------------
363
364/**
365 * Convert Accented Foreign Characters to ASCII
366 *
367 * @access public
368 * @param string the text string
369 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200370 */
Derek Jonesdaf9c012010-03-05 10:29:30 -0600371if ( ! function_exists('convert_accented_characters'))
372{
Eric Barnes5e044802011-01-11 16:10:26 -0500373 function convert_accented_characters($str)
Derek Jonesdaf9c012010-03-05 10:29:30 -0600374 {
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200375 global $foreign_characters;
Barry Mienydd671972010-10-04 16:33:58 +0200376
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200377 if ( ! isset($foreign_characters) OR ! is_array($foreign_characters))
Derek Jonesdaf9c012010-03-05 10:29:30 -0600378 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200379 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200380 {
381 include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
382 }
383 elseif (is_file(APPPATH.'config/foreign_chars.php'))
384 {
385 include(APPPATH.'config/foreign_chars.php');
386 }
387
388 if ( ! isset($foreign_characters) OR ! is_array($foreign_chars))
389 {
390 return $str;
391 }
Derek Jonesdaf9c012010-03-05 10:29:30 -0600392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Eric Barnes5e044802011-01-11 16:10:26 -0500394 return preg_replace(array_keys($foreign_characters), array_values($foreign_characters), $str);
Derek Jonesdaf9c012010-03-05 10:29:30 -0600395 }
396}
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398// ------------------------------------------------------------------------
399
400/**
401 * Word Wrap
402 *
Andrey Andreev09375d72012-01-19 14:57:46 +0200403 * Wraps text at the specified character. Maintains the integrity of words.
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
405 * will URLs.
406 *
407 * @access public
408 * @param string the text string
409 * @param integer the number of characters to wrap at
410 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200411 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000412if ( ! function_exists('word_wrap'))
413{
Andrey Andreevfc443552012-01-07 02:14:55 +0200414 function word_wrap($str, $charlim = 76)
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200416 // Set the character limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 if ( ! is_numeric($charlim))
Andrey Andreev4921fed2012-01-07 01:28:07 +0200418 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 $charlim = 76;
Andrey Andreev4921fed2012-01-07 01:28:07 +0200420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 // Reduce multiple spaces
Andrey Andreev4921fed2012-01-07 01:28:07 +0200423 $str = preg_replace('| +|', ' ', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 // Standardize newlines
426 if (strpos($str, "\r") !== FALSE)
427 {
Barry Mienydd671972010-10-04 16:33:58 +0200428 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
431 // If the current word is surrounded by {unwrap} tags we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 // strip the entire chunk and replace it with a marker.
433 $unwrap = array();
Andrey Andreev4921fed2012-01-07 01:28:07 +0200434 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200436 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200438 $unwrap[] = $matches[1][$i];
439 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
443 // Use PHP's native function to do the initial wordwrap.
444 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev4921fed2012-01-07 01:28:07 +0200445 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 $str = wordwrap($str, $charlim, "\n", FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 // Split the string into individual lines of text and cycle through them
Andrey Andreev4921fed2012-01-07 01:28:07 +0200449 $output = '';
Barry Mienydd671972010-10-04 16:33:58 +0200450 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
452 // Is the line within the allowed character count?
453 // If so we'll join it to the output and continue
454 if (strlen($line) <= $charlim)
455 {
Barry Mienydd671972010-10-04 16:33:58 +0200456 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 continue;
458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 $temp = '';
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500461 while ((strlen($line)) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
463 // If the over-length word is a URL we won't wrap it
Andrey Andreev4921fed2012-01-07 01:28:07 +0200464 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
466 break;
467 }
468
469 // Trim the word down
470 $temp .= substr($line, 0, $charlim-1);
471 $line = substr($line, $charlim-1);
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
474 // If $temp contains data it means we had to split up an over-length
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 // word into smaller chunks so we'll add it back to our current line
476 if ($temp != '')
477 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200478 $output .= $temp."\n".$line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
480 else
481 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200482 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
485
486 // Put our markers back
487 if (count($unwrap) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200488 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 foreach ($unwrap as $key => $val)
490 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200491 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
493 }
494
Andrey Andreev4921fed2012-01-07 01:28:07 +0200495 // Remove the unwrap tags and return
496 return str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
498}
Barry Mienydd671972010-10-04 16:33:58 +0200499
Greg Akercbe32472010-08-05 14:09:20 -0500500// ------------------------------------------------------------------------
501
Greg Aker0f6b7c12010-08-05 14:11:14 -0500502/**
503 * Ellipsize String
504 *
505 * This function will strip tags from a string, split it at its max_length and ellipsize
506 *
Barry Mienydd671972010-10-04 16:33:58 +0200507 * @param string string to ellipsize
Greg Aker0f6b7c12010-08-05 14:11:14 -0500508 * @param integer max length of string
509 * @param mixed int (1|0) or float, .5, .2, etc for position to split
Barry Mienydd671972010-10-04 16:33:58 +0200510 * @param string ellipsis ; Default '...'
Greg Aker0f6b7c12010-08-05 14:11:14 -0500511 * @return string ellipsized string
512 */
Greg Akercbe32472010-08-05 14:09:20 -0500513if ( ! function_exists('ellipsize'))
514{
Greg Akercbe32472010-08-05 14:09:20 -0500515 function ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
516 {
517 // Strip tags
518 $str = trim(strip_tags($str));
519
520 // Is the string long enough to ellipsize?
521 if (strlen($str) <= $max_length)
522 {
523 return $str;
524 }
525
526 $beg = substr($str, 0, floor($max_length * $position));
Greg Akercbe32472010-08-05 14:09:20 -0500527 $position = ($position > 1) ? 1 : $position;
528
529 if ($position === 1)
530 {
531 $end = substr($str, 0, -($max_length - strlen($beg)));
532 }
533 else
534 {
535 $end = substr($str, -($max_length - strlen($beg)));
536 }
537
Barry Mienydd671972010-10-04 16:33:58 +0200538 return $beg.$ellipsis.$end;
Greg Akercbe32472010-08-05 14:09:20 -0500539 }
540}
Derek Allard2067d1a2008-11-13 22:59:24 +0000541
542/* End of file text_helper.php */
Andrey Andreev4921fed2012-01-07 01:28:07 +0200543/* Location: ./system/helpers/text_helper.php */