blob: 1d0605ddc9cebe7dd750c0f2177d28b0092602f1 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Text Helpers
31 *
32 * @package CodeIgniter
33 * @subpackage Helpers
34 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/helpers/text_helper.html
37 */
38
39// ------------------------------------------------------------------------
40
Derek Allard2067d1a2008-11-13 22:59:24 +000041if ( ! function_exists('word_limiter'))
42{
Timothy Warrenb75faa12012-04-27 12:03:32 -040043 /**
44 * Word Limiter
45 *
46 * Limits a string to X number of words.
47 *
48 * @param string
49 * @param int
50 * @param string the end character. Usually an ellipsis
51 * @return string
52 */
Derek Allard2067d1a2008-11-13 22:59:24 +000053 function word_limiter($str, $limit = 100, $end_char = '&#8230;')
54 {
Alex Bilbie773ccc32012-06-02 11:11:08 +010055 if (trim($str) === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000056 {
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
Derek Allard2067d1a2008-11-13 22:59:24 +000073if ( ! function_exists('character_limiter'))
74{
Timothy Warrenb75faa12012-04-27 12:03:32 -040075 /**
76 * Character Limiter
77 *
78 * Limits the string based on the character count. Preserves complete words
79 * so the character count may not be exactly as specified.
80 *
81 * @param string
82 * @param int
83 * @param string the end character. Usually an ellipsis
84 * @return string
85 */
Derek Allard2067d1a2008-11-13 22:59:24 +000086 function character_limiter($str, $n = 500, $end_char = '&#8230;')
87 {
Mohammad Javad Naderic6f5ed12013-08-22 18:33:50 +043088 if (mb_strlen($str) < $n)
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
90 return $str;
91 }
Barry Mienydd671972010-10-04 16:33:58 +020092
vlakoff62a5ee32012-09-04 23:12:49 +020093 // a bit complicated, but faster than preg_replace with \s+
94 $str = preg_replace('/ {2,}/', ' ', str_replace(array("\r", "\n", "\t", "\x0B", "\x0C"), ' ', $str));
Derek Allard2067d1a2008-11-13 22:59:24 +000095
Mohammad Javad Naderic6f5ed12013-08-22 18:33:50 +043096 if (mb_strlen($str) <= $n)
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
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
Mohammad Javad Naderic6f5ed12013-08-22 18:33:50 +0430106 if (mb_strlen($out) >= $n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
Derek Jones01d6b4f2009-02-03 14:51:00 +0000108 $out = trim($out);
Mohammad Javad Naderic6f5ed12013-08-22 18:33:50 +0430109 return (mb_strlen($out) === mb_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
Derek Allard2067d1a2008-11-13 22:59:24 +0000117if ( ! function_exists('ascii_to_entities'))
118{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400119 /**
120 * High ASCII to Entities
121 *
Andrey Andreev7d753462012-10-27 03:37:40 +0300122 * Converts high ASCII text and MS Word special characters to character entities
Timothy Warrenb75faa12012-04-27 12:03:32 -0400123 *
Andrey Andreev7d753462012-10-27 03:37:40 +0300124 * @param string $str
Timothy Warrenb75faa12012-04-27 12:03:32 -0400125 * @return string
126 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 function ascii_to_entities($str)
128 {
Andrey Andreev7d753462012-10-27 03:37:40 +0300129 $out = '';
Andrey Andreev40235e62014-01-09 14:20:57 +0200130 for ($i = 0, $s = strlen($str) - 1, $count = 1, $temp = array(); $i <= $s; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200131 {
132 $ordinal = ord($str[$i]);
133
134 if ($ordinal < 128)
135 {
Derek Jones1978e122009-02-03 14:54:43 +0000136 /*
137 If the $temp array has a value but we have moved on, then it seems only
138 fair that we output that entity and restart $temp before continuing. -Paul
139 */
Andrey Andreev4921fed2012-01-07 01:28:07 +0200140 if (count($temp) === 1)
Derek Jones1978e122009-02-03 14:54:43 +0000141 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200142 $out .= '&#'.array_shift($temp).';';
Derek Jones1978e122009-02-03 14:54:43 +0000143 $count = 1;
144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Jones1978e122009-02-03 14:54:43 +0000146 $out .= $str[$i];
Barry Mienydd671972010-10-04 16:33:58 +0200147 }
148 else
149 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200150 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200151 {
152 $count = ($ordinal < 224) ? 2 : 3;
153 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000154
Barry Mienydd671972010-10-04 16:33:58 +0200155 $temp[] = $ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000156
Andrey Andreev4921fed2012-01-07 01:28:07 +0200157 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200158 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200159 $number = ($count === 3)
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200160 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
161 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
Barry Mienydd671972010-10-04 16:33:58 +0200162
163 $out .= '&#'.$number.';';
164 $count = 1;
165 $temp = array();
166 }
Andrey Andreev40235e62014-01-09 14:20:57 +0200167 // If this is the last iteration, just output whatever we have
168 elseif ($i === $s)
169 {
170 $out .= '&#'.implode(';', $temp).';';
171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172 }
173 }
174
175 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
177}
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179// ------------------------------------------------------------------------
180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181if ( ! function_exists('entities_to_ascii'))
182{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400183 /**
184 * Entities to ASCII
185 *
186 * Converts character entities back to ASCII
187 *
188 * @param string
189 * @param bool
190 * @return string
191 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 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 Andreevea41c8a2014-02-26 18:31:02 +0200223 return str_replace(
224 array('&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#45;'),
225 array('&', '<', '>', '"', "'", '-'),
226 $str
227 );
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000236if ( ! function_exists('word_censor'))
237{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400238 /**
239 * Word Censoring Function
240 *
241 * Supply a string and an array of disallowed words and any
242 * matched words will be converted to #### or to the replacement
243 * word you've submitted.
244 *
245 * @param string the text string
246 * @param string the array of censoered words
247 * @param string the optional replacement value
248 * @return string
249 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 function word_censor($str, $censored, $replacement = '')
251 {
252 if ( ! is_array($censored))
253 {
254 return $str;
255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
257 $str = ' '.$str.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000258
Derek Jonesf1b721a2009-01-21 17:52:13 +0000259 // \w, \b and a few others do not match on a unicode character
260 // set for performance reasons. As a result words like über
261 // will not match on a word boundary. Instead, we'll assume that
Derek Jonesdaf9c012010-03-05 10:29:30 -0600262 // a bad word will be bookeneded by any of these characters.
Derek Jonesf1b721a2009-01-21 17:52:13 +0000263 $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 foreach ($censored as $badword)
266 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100267 if ($replacement !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000269 $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/i", "\\1{$replacement}\\3", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
271 else
272 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000273 $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 +0000274 }
275 }
Derek Jonesf1b721a2009-01-21 17:52:13 +0000276
Barry Mienydd671972010-10-04 16:33:58 +0200277 return trim($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
279}
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281// ------------------------------------------------------------------------
282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283if ( ! function_exists('highlight_code'))
284{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400285 /**
286 * Code Highlighter
287 *
288 * Colorizes code strings
289 *
290 * @param string the text string
291 * @return string
292 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 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 */
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200302 $str = str_replace(
303 array('&lt;', '&gt;', '<?', '?>', '<%', '%>', '\\', '</script>'),
304 array('<', '>', 'phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
305 $str
306 );
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);
Derek Allard2067d1a2008-11-13 22:59:24 +0000311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // Remove our artificially added PHP, and the syntax highlighting that came with it
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200313 $str = preg_replace(
314 array(
315 '/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i',
316 '/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is',
317 '/<span style="color: #[A-Z0-9]+"\><\/span>/i'
318 ),
319 array(
320 '<span style="color: #$1">',
321 "$1</span>\n</span>\n</code>",
322 ''
323 ),
324 $str
325 );
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // Replace our markers back to PHP tags.
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200328 return str_replace(
329 array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
330 array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'),
331 $str
332 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
334}
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336// ------------------------------------------------------------------------
337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338if ( ! function_exists('highlight_phrase'))
339{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400340 /**
341 * Phrase Highlighter
342 *
343 * Highlights a phrase within a text string
344 *
Andrey Andreevac023e12014-01-07 16:13:03 +0200345 * @param string $str the text string
346 * @param string $phrase the phrase you'd like to highlight
347 * @param string $tag_open the openging tag to precede the phrase with
348 * @param string $tag_close the closing tag to end the phrase with
Timothy Warrenb75faa12012-04-27 12:03:32 -0400349 * @return string
350 */
Andrey Andreevac023e12014-01-07 16:13:03 +0200351 function highlight_phrase($str, $phrase, $tag_open = '<mark>', $tag_close = '</mark>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
Andrey Andreevac023e12014-01-07 16:13:03 +0200353 return ($str !== '' && $phrase !== '')
Ivan Tcholakov8bc73eb2014-03-17 05:18:45 +0200354 ? preg_replace('/('.preg_quote($phrase, '/').')/i'.(UTF8_ENABLED ? 'u' : ''), $tag_open.'\\1'.$tag_close, $str)
Andrey Andreevac023e12014-01-07 16:13:03 +0200355 : $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
357}
Derek Jonesdaf9c012010-03-05 10:29:30 -0600358
359// ------------------------------------------------------------------------
360
Derek Jonesdaf9c012010-03-05 10:29:30 -0600361if ( ! function_exists('convert_accented_characters'))
362{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400363 /**
364 * Convert Accented Foreign Characters to ASCII
365 *
Andrey Andreev06879112013-01-29 15:05:02 +0200366 * @param string $str Input string
Timothy Warrenb75faa12012-04-27 12:03:32 -0400367 * @return string
368 */
Eric Barnes5e044802011-01-11 16:10:26 -0500369 function convert_accented_characters($str)
Derek Jonesdaf9c012010-03-05 10:29:30 -0600370 {
vlakofff5b4f6a2013-02-28 22:17:51 +0100371 static $array_from, $array_to;
Barry Mienydd671972010-10-04 16:33:58 +0200372
vlakofff5b4f6a2013-02-28 22:17:51 +0100373 if ( ! is_array($array_from))
Derek Jonesdaf9c012010-03-05 10:29:30 -0600374 {
Andrey Andreev06879112013-01-29 15:05:02 +0200375 if (file_exists(APPPATH.'config/foreign_chars.php'))
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200376 {
377 include(APPPATH.'config/foreign_chars.php');
378 }
379
Andrey Andreev06879112013-01-29 15:05:02 +0200380 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200381 {
Andrey Andreev06879112013-01-29 15:05:02 +0200382 include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
383 }
384
385 if (empty($foreign_characters) OR ! is_array($foreign_characters))
386 {
vlakofff5b4f6a2013-02-28 22:17:51 +0100387 $array_from = array();
388 $array_to = array();
389
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200390 return $str;
391 }
Andrey Andreev06879112013-01-29 15:05:02 +0200392
vlakofff5b4f6a2013-02-28 22:17:51 +0100393 $array_from = array_keys($foreign_characters);
394 $array_to = array_values($foreign_characters);
Derek Jonesdaf9c012010-03-05 10:29:30 -0600395 }
Barry Mienydd671972010-10-04 16:33:58 +0200396
vlakofff5b4f6a2013-02-28 22:17:51 +0100397 return preg_replace($array_from, $array_to, $str);
Derek Jonesdaf9c012010-03-05 10:29:30 -0600398 }
399}
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401// ------------------------------------------------------------------------
402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403if ( ! function_exists('word_wrap'))
404{
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300405 /**
406 * Word Wrap
407 *
408 * Wraps text at the specified character. Maintains the integrity of words.
409 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
410 * will URLs.
411 *
412 * @param string $str the text string
413 * @param int $charlim = 76 the number of characters to wrap at
414 * @return string
415 */
Andrey Andreevfc443552012-01-07 02:14:55 +0200416 function word_wrap($str, $charlim = 76)
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200418 // Set the character limit
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200419 is_numeric($charlim) OR $charlim = 76;
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // Reduce multiple spaces
Andrey Andreev4921fed2012-01-07 01:28:07 +0200422 $str = preg_replace('| +|', ' ', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 // Standardize newlines
425 if (strpos($str, "\r") !== FALSE)
426 {
Barry Mienydd671972010-10-04 16:33:58 +0200427 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
Barry Mienydd671972010-10-04 16:33:58 +0200429
430 // If the current word is surrounded by {unwrap} tags we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 // strip the entire chunk and replace it with a marker.
432 $unwrap = array();
vlakoff0f7eba22014-05-20 10:32:44 +0200433 if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200435 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200437 $unwrap[] = $matches[1][$i];
vlakoff0f7eba22014-05-20 10:32:44 +0200438 $str = str_replace($matches[0][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
440 }
Barry Mienydd671972010-10-04 16:33:58 +0200441
442 // Use PHP's native function to do the initial wordwrap.
443 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev4921fed2012-01-07 01:28:07 +0200444 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 $str = wordwrap($str, $charlim, "\n", FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 // Split the string into individual lines of text and cycle through them
Andrey Andreev4921fed2012-01-07 01:28:07 +0200448 $output = '';
Barry Mienydd671972010-10-04 16:33:58 +0200449 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
451 // Is the line within the allowed character count?
452 // If so we'll join it to the output and continue
Andrey Andreev6ce47462014-02-13 03:28:00 +0200453 if (mb_strlen($line) <= $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
Barry Mienydd671972010-10-04 16:33:58 +0200455 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 continue;
457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 $temp = '';
Andrey Andreev6ce47462014-02-13 03:28:00 +0200460 while (mb_strlen($line) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
462 // If the over-length word is a URL we won't wrap it
vlakoff2a8560c2014-05-20 10:32:35 +0200463 if (preg_match('!\[url.+\]|://|www\.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
465 break;
466 }
467
468 // Trim the word down
Andrey Andreev6ce47462014-02-13 03:28:00 +0200469 $temp .= mb_substr($line, 0, $charlim - 1);
470 $line = mb_substr($line, $charlim - 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
473 // If $temp contains data it means we had to split up an over-length
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 // word into smaller chunks so we'll add it back to our current line
Alex Bilbie773ccc32012-06-02 11:11:08 +0100475 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200477 $output .= $temp."\n".$line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 }
479 else
480 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200481 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 }
484
485 // Put our markers back
486 if (count($unwrap) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200487 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 foreach ($unwrap as $key => $val)
489 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200490 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 }
492 }
493
vlakoff0f7eba22014-05-20 10:32:44 +0200494 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
496}
Barry Mienydd671972010-10-04 16:33:58 +0200497
Greg Akercbe32472010-08-05 14:09:20 -0500498// ------------------------------------------------------------------------
499
500if ( ! function_exists('ellipsize'))
501{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400502 /**
503 * Ellipsize String
504 *
505 * This function will strip tags from a string, split it at its max_length and ellipsize
506 *
507 * @param string string to ellipsize
508 * @param int max length of string
509 * @param mixed int (1|0) or float, .5, .2, etc for position to split
510 * @param string ellipsis ; Default '...'
511 * @return string ellipsized string
512 */
Greg Akercbe32472010-08-05 14:09:20 -0500513 function ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
514 {
515 // Strip tags
516 $str = trim(strip_tags($str));
517
518 // Is the string long enough to ellipsize?
Andrey Andreev6ce47462014-02-13 03:28:00 +0200519 if (mb_strlen($str) <= $max_length)
Greg Akercbe32472010-08-05 14:09:20 -0500520 {
521 return $str;
522 }
523
Andrey Andreev6ce47462014-02-13 03:28:00 +0200524 $beg = mb_substr($str, 0, floor($max_length * $position));
Greg Akercbe32472010-08-05 14:09:20 -0500525 $position = ($position > 1) ? 1 : $position;
526
527 if ($position === 1)
528 {
Andrey Andreev6ce47462014-02-13 03:28:00 +0200529 $end = mb_substr($str, 0, -($max_length - mb_strlen($beg)));
Greg Akercbe32472010-08-05 14:09:20 -0500530 }
531 else
532 {
Andrey Andreev6ce47462014-02-13 03:28:00 +0200533 $end = mb_substr($str, -($max_length - mb_strlen($beg)));
Greg Akercbe32472010-08-05 14:09:20 -0500534 }
535
Barry Mienydd671972010-10-04 16:33:58 +0200536 return $beg.$ellipsis.$end;
Greg Akercbe32472010-08-05 14:09:20 -0500537 }
538}
Derek Allard2067d1a2008-11-13 22:59:24 +0000539
540/* End of file text_helper.php */
Andrey Andreeve684bda2012-03-26 13:47:29 +0300541/* Location: ./system/helpers/text_helper.php */