blob: 3a847f29b19f4b5bb2b1476903d16329ab5eec33 [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
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
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter Text Helpers
32 *
33 * @package CodeIgniter
34 * @subpackage Helpers
35 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/helpers/text_helper.html
38 */
39
40// ------------------------------------------------------------------------
41
42/**
43 * Word Limiter
44 *
45 * Limits a string to X number of words.
46 *
47 * @access public
48 * @param string
49 * @param integer
50 * @param string the end character. Usually an ellipsis
51 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020052 */
Derek Allard2067d1a2008-11-13 22:59:24 +000053if ( ! function_exists('word_limiter'))
54{
55 function word_limiter($str, $limit = 100, $end_char = '&#8230;')
56 {
57 if (trim($str) == '')
58 {
59 return $str;
60 }
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
Barry Mienydd671972010-10-04 16:33:58 +020063
Andrey Andreev4921fed2012-01-07 01:28:07 +020064 if (strlen($str) === strlen($matches[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +000065 {
66 $end_char = '';
67 }
Barry Mienydd671972010-10-04 16:33:58 +020068
Derek Allard2067d1a2008-11-13 22:59:24 +000069 return rtrim($matches[0]).$end_char;
70 }
71}
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073// ------------------------------------------------------------------------
74
75/**
76 * Character Limiter
77 *
Derek Jones37f4b9c2011-07-01 17:56:50 -050078 * Limits the string based on the character count. Preserves complete words
Derek Allard2067d1a2008-11-13 22:59:24 +000079 * so the character count may not be exactly as specified.
80 *
81 * @access public
82 * @param string
83 * @param integer
84 * @param string the end character. Usually an ellipsis
85 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020086 */
Derek Allard2067d1a2008-11-13 22:59:24 +000087if ( ! function_exists('character_limiter'))
88{
89 function character_limiter($str, $n = 500, $end_char = '&#8230;')
90 {
91 if (strlen($str) < $n)
92 {
93 return $str;
94 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Allard2067d1a2008-11-13 22:59:24 +000096 $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
97
98 if (strlen($str) <= $n)
99 {
100 return $str;
101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Andrey Andreev4921fed2012-01-07 01:28:07 +0200103 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 foreach (explode(' ', trim($str)) as $val)
105 {
Derek Jones01d6b4f2009-02-03 14:51:00 +0000106 $out .= $val.' ';
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 if (strlen($out) >= $n)
109 {
Derek Jones01d6b4f2009-02-03 14:51:00 +0000110 $out = trim($out);
111 return (strlen($out) == strlen($str)) ? $out : $out.$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200112 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114 }
115}
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117// ------------------------------------------------------------------------
118
119/**
120 * High ASCII to Entities
121 *
122 * Converts High ascii text and MS Word special characters to character entities
123 *
124 * @access public
125 * @param string
126 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200127 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000128if ( ! function_exists('ascii_to_entities'))
129{
130 function ascii_to_entities($str)
131 {
Barry Mienydd671972010-10-04 16:33:58 +0200132 $count = 1;
133 $out = '';
134 $temp = array();
135
136 for ($i = 0, $s = strlen($str); $i < $s; $i++)
137 {
138 $ordinal = ord($str[$i]);
139
140 if ($ordinal < 128)
141 {
Derek Jones1978e122009-02-03 14:54:43 +0000142 /*
143 If the $temp array has a value but we have moved on, then it seems only
144 fair that we output that entity and restart $temp before continuing. -Paul
145 */
Andrey Andreev4921fed2012-01-07 01:28:07 +0200146 if (count($temp) === 1)
Derek Jones1978e122009-02-03 14:54:43 +0000147 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500148 $out .= '&#'.array_shift($temp).';';
Derek Jones1978e122009-02-03 14:54:43 +0000149 $count = 1;
150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Jones1978e122009-02-03 14:54:43 +0000152 $out .= $str[$i];
Barry Mienydd671972010-10-04 16:33:58 +0200153 }
154 else
155 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200156 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200157 {
158 $count = ($ordinal < 224) ? 2 : 3;
159 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000160
Barry Mienydd671972010-10-04 16:33:58 +0200161 $temp[] = $ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000162
Andrey Andreev4921fed2012-01-07 01:28:07 +0200163 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200164 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200165 $number = ($count === 3)
166 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
167 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
Barry Mienydd671972010-10-04 16:33:58 +0200168
169 $out .= '&#'.$number.';';
170 $count = 1;
171 $temp = array();
172 }
173 }
174 }
175
176 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 }
178}
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180// ------------------------------------------------------------------------
181
182/**
183 * Entities to ASCII
184 *
185 * Converts character entities back to ASCII
186 *
187 * @access public
188 * @param string
189 * @param bool
190 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200191 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000192if ( ! function_exists('entities_to_ascii'))
193{
194 function entities_to_ascii($str, $all = TRUE)
195 {
Barry Mienydd671972010-10-04 16:33:58 +0200196 if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
197 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200198 for ($i = 0, $s = count($matches[0]); $i < $s; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200199 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200200 $digits = $matches[1][$i];
Barry Mienydd671972010-10-04 16:33:58 +0200201 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000202
Barry Mienydd671972010-10-04 16:33:58 +0200203 if ($digits < 128)
204 {
205 $out .= chr($digits);
Derek Allard2067d1a2008-11-13 22:59:24 +0000206
Barry Mienydd671972010-10-04 16:33:58 +0200207 }
208 elseif ($digits < 2048)
209 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200210 $out .= chr(192 + (($digits - ($digits % 64)) / 64)).chr(128 + ($digits % 64));
Barry Mienydd671972010-10-04 16:33:58 +0200211 }
212 else
213 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200214 $out .= chr(224 + (($digits - ($digits % 4096)) / 4096))
215 .chr(128 + ((($digits % 4096) - ($digits % 64)) / 64))
216 .chr(128 + ($digits % 64));
Barry Mienydd671972010-10-04 16:33:58 +0200217 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000218
Andrey Andreev4921fed2012-01-07 01:28:07 +0200219 $str = str_replace($matches[0][$i], $out, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200220 }
221 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000222
Barry Mienydd671972010-10-04 16:33:58 +0200223 if ($all)
224 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200225 return str_replace(array('&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#45;'),
226 array('&', '<', '>', '"', "'", '-'),
227 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200228 }
229
230 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
232}
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234// ------------------------------------------------------------------------
235
236/**
237 * Word Censoring Function
238 *
239 * Supply a string and an array of disallowed words and any
240 * matched words will be converted to #### or to the replacement
241 * word you've submitted.
242 *
243 * @access public
244 * @param string the text string
245 * @param string the array of censoered words
246 * @param string the optional replacement value
247 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200248 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000249if ( ! function_exists('word_censor'))
250{
251 function word_censor($str, $censored, $replacement = '')
252 {
253 if ( ! is_array($censored))
254 {
255 return $str;
256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
258 $str = ' '.$str.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000259
Derek Jonesf1b721a2009-01-21 17:52:13 +0000260 // \w, \b and a few others do not match on a unicode character
261 // set for performance reasons. As a result words like über
262 // will not match on a word boundary. Instead, we'll assume that
Derek Jonesdaf9c012010-03-05 10:29:30 -0600263 // a bad word will be bookeneded by any of these characters.
Derek Jonesf1b721a2009-01-21 17:52:13 +0000264 $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 foreach ($censored as $badword)
267 {
268 if ($replacement != '')
269 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000270 $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/i", "\\1{$replacement}\\3", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
272 else
273 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000274 $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/ie", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 }
276 }
Derek Jonesf1b721a2009-01-21 17:52:13 +0000277
Barry Mienydd671972010-10-04 16:33:58 +0200278 return trim($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
280}
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282// ------------------------------------------------------------------------
283
284/**
285 * Code Highlighter
286 *
287 * Colorizes code strings
288 *
289 * @access public
290 * @param string the text string
291 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200292 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000293if ( ! function_exists('highlight_code'))
294{
295 function highlight_code($str)
Barry Mienydd671972010-10-04 16:33:58 +0200296 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200297 /* The highlight string function encodes and highlights
298 * brackets so we need them to start raw.
299 *
300 * Also replace any existing PHP tags to temporary markers
301 * so they don't accidentally break the string out of PHP,
302 * and thus, thwart the highlighting.
303 */
304 $str = str_replace(array('&lt;', '&gt;', '<?', '?>', '<%', '%>', '\\', '</script>'),
305 array('<', '>', 'phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
306 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000307
308 // The highlight_string function requires that the text be surrounded
309 // by PHP tags, which we will remove later
Andrey Andreev4921fed2012-01-07 01:28:07 +0200310 $str = highlight_string('<?php '.$str.' ?>', TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // Remove our artificially added PHP, and the syntax highlighting that came with it
Andrey Andreev4921fed2012-01-07 01:28:07 +0200313 $str = preg_replace(array(
314 '/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i',
315 '/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is',
316 '/<span style="color: #[A-Z0-9]+"\><\/span>/i'
317 ),
318 array(
319 '<span style="color: #$1">',
320 "$1</span>\n</span>\n</code>",
321 ''
322 ),
323 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // Replace our markers back to PHP tags.
Andrey Andreev4921fed2012-01-07 01:28:07 +0200326 return str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
327 array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'),
328 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
330}
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332// ------------------------------------------------------------------------
333
334/**
335 * Phrase Highlighter
336 *
337 * Highlights a phrase within a text string
338 *
339 * @access public
340 * @param string the text string
341 * @param string the phrase you'd like to highlight
342 * @param string the openging tag to precede the phrase with
343 * @param string the closing tag to end the phrase with
344 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200345 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000346if ( ! function_exists('highlight_phrase'))
347{
348 function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
349 {
350 if ($str == '')
351 {
352 return '';
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 if ($phrase != '')
356 {
357 return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);
358 }
359
360 return $str;
361 }
362}
Derek Jonesdaf9c012010-03-05 10:29:30 -0600363
364// ------------------------------------------------------------------------
365
366/**
367 * Convert Accented Foreign Characters to ASCII
368 *
369 * @access public
370 * @param string the text string
371 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200372 */
Derek Jonesdaf9c012010-03-05 10:29:30 -0600373if ( ! function_exists('convert_accented_characters'))
374{
Eric Barnes5e044802011-01-11 16:10:26 -0500375 function convert_accented_characters($str)
Derek Jonesdaf9c012010-03-05 10:29:30 -0600376 {
Greg Akerd96f8822011-12-27 16:23:47 -0600377 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
378 {
379 include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
380 }
381 elseif (is_file(APPPATH.'config/foreign_chars.php'))
382 {
383 include(APPPATH.'config/foreign_chars.php');
384 }
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Jonesdaf9c012010-03-05 10:29:30 -0600386 if ( ! isset($foreign_characters))
387 {
Eric Barnes5e044802011-01-11 16:10:26 -0500388 return $str;
Derek Jonesdaf9c012010-03-05 10:29:30 -0600389 }
Barry Mienydd671972010-10-04 16:33:58 +0200390
Eric Barnes5e044802011-01-11 16:10:26 -0500391 return preg_replace(array_keys($foreign_characters), array_values($foreign_characters), $str);
Derek Jonesdaf9c012010-03-05 10:29:30 -0600392 }
393}
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395// ------------------------------------------------------------------------
396
397/**
398 * Word Wrap
399 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500400 * Wraps text at the specified character. Maintains the integrity of words.
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
402 * will URLs.
403 *
404 * @access public
405 * @param string the text string
406 * @param integer the number of characters to wrap at
407 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200408 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000409if ( ! function_exists('word_wrap'))
410{
Andrey Andreevfc443552012-01-07 02:14:55 +0200411 function word_wrap($str, $charlim = 76)
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200413 // Set the character limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 if ( ! is_numeric($charlim))
Andrey Andreev4921fed2012-01-07 01:28:07 +0200415 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 $charlim = 76;
Andrey Andreev4921fed2012-01-07 01:28:07 +0200417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 // Reduce multiple spaces
Andrey Andreev4921fed2012-01-07 01:28:07 +0200420 $str = preg_replace('| +|', ' ', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 // Standardize newlines
423 if (strpos($str, "\r") !== FALSE)
424 {
Barry Mienydd671972010-10-04 16:33:58 +0200425 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
428 // If the current word is surrounded by {unwrap} tags we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 // strip the entire chunk and replace it with a marker.
430 $unwrap = array();
Andrey Andreev4921fed2012-01-07 01:28:07 +0200431 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200433 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200435 $unwrap[] = $matches[1][$i];
436 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
438 }
Barry Mienydd671972010-10-04 16:33:58 +0200439
440 // Use PHP's native function to do the initial wordwrap.
441 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev4921fed2012-01-07 01:28:07 +0200442 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 $str = wordwrap($str, $charlim, "\n", FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 // Split the string into individual lines of text and cycle through them
Andrey Andreev4921fed2012-01-07 01:28:07 +0200446 $output = '';
Barry Mienydd671972010-10-04 16:33:58 +0200447 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
449 // Is the line within the allowed character count?
450 // If so we'll join it to the output and continue
451 if (strlen($line) <= $charlim)
452 {
Barry Mienydd671972010-10-04 16:33:58 +0200453 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 continue;
455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 $temp = '';
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500458 while ((strlen($line)) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
460 // If the over-length word is a URL we won't wrap it
Andrey Andreev4921fed2012-01-07 01:28:07 +0200461 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
463 break;
464 }
465
466 // Trim the word down
467 $temp .= substr($line, 0, $charlim-1);
468 $line = substr($line, $charlim-1);
469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
471 // If $temp contains data it means we had to split up an over-length
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 // word into smaller chunks so we'll add it back to our current line
473 if ($temp != '')
474 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200475 $output .= $temp."\n".$line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
477 else
478 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200479 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
482
483 // Put our markers back
484 if (count($unwrap) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200485 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 foreach ($unwrap as $key => $val)
487 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200488 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
490 }
491
Andrey Andreev4921fed2012-01-07 01:28:07 +0200492 // Remove the unwrap tags and return
493 return str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
495}
Barry Mienydd671972010-10-04 16:33:58 +0200496
Greg Akercbe32472010-08-05 14:09:20 -0500497// ------------------------------------------------------------------------
498
Greg Aker0f6b7c12010-08-05 14:11:14 -0500499/**
500 * Ellipsize String
501 *
502 * This function will strip tags from a string, split it at its max_length and ellipsize
503 *
Barry Mienydd671972010-10-04 16:33:58 +0200504 * @param string string to ellipsize
Greg Aker0f6b7c12010-08-05 14:11:14 -0500505 * @param integer max length of string
506 * @param mixed int (1|0) or float, .5, .2, etc for position to split
Barry Mienydd671972010-10-04 16:33:58 +0200507 * @param string ellipsis ; Default '...'
Greg Aker0f6b7c12010-08-05 14:11:14 -0500508 * @return string ellipsized string
509 */
Greg Akercbe32472010-08-05 14:09:20 -0500510if ( ! function_exists('ellipsize'))
511{
Greg Akercbe32472010-08-05 14:09:20 -0500512 function ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
513 {
514 // Strip tags
515 $str = trim(strip_tags($str));
516
517 // Is the string long enough to ellipsize?
518 if (strlen($str) <= $max_length)
519 {
520 return $str;
521 }
522
523 $beg = substr($str, 0, floor($max_length * $position));
Greg Akercbe32472010-08-05 14:09:20 -0500524 $position = ($position > 1) ? 1 : $position;
525
526 if ($position === 1)
527 {
528 $end = substr($str, 0, -($max_length - strlen($beg)));
529 }
530 else
531 {
532 $end = substr($str, -($max_length - strlen($beg)));
533 }
534
Barry Mienydd671972010-10-04 16:33:58 +0200535 return $beg.$ellipsis.$end;
Greg Akercbe32472010-08-05 14:09:20 -0500536 }
537}
Derek Allard2067d1a2008-11-13 22:59:24 +0000538
539/* End of file text_helper.php */
Andrey Andreev4921fed2012-01-07 01:28:07 +0200540/* Location: ./system/helpers/text_helper.php */