blob: 705238704392f095f8557425f088913524ef25f1 [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
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 */
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 {
88 if (strlen($str) < $n)
89 {
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
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
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 = '';
130 for ($i = 0, $s = strlen($str), $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)
160 ? (($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 }
167 }
168 }
169
170 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
172}
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174// ------------------------------------------------------------------------
175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176if ( ! function_exists('entities_to_ascii'))
177{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400178 /**
179 * Entities to ASCII
180 *
181 * Converts character entities back to ASCII
182 *
183 * @param string
184 * @param bool
185 * @return string
186 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 function entities_to_ascii($str, $all = TRUE)
188 {
Barry Mienydd671972010-10-04 16:33:58 +0200189 if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
190 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200191 for ($i = 0, $s = count($matches[0]); $i < $s; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200192 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200193 $digits = $matches[1][$i];
Barry Mienydd671972010-10-04 16:33:58 +0200194 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000195
Barry Mienydd671972010-10-04 16:33:58 +0200196 if ($digits < 128)
197 {
198 $out .= chr($digits);
Derek Allard2067d1a2008-11-13 22:59:24 +0000199
Barry Mienydd671972010-10-04 16:33:58 +0200200 }
201 elseif ($digits < 2048)
202 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200203 $out .= chr(192 + (($digits - ($digits % 64)) / 64)).chr(128 + ($digits % 64));
Barry Mienydd671972010-10-04 16:33:58 +0200204 }
205 else
206 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200207 $out .= chr(224 + (($digits - ($digits % 4096)) / 4096))
208 .chr(128 + ((($digits % 4096) - ($digits % 64)) / 64))
209 .chr(128 + ($digits % 64));
Barry Mienydd671972010-10-04 16:33:58 +0200210 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000211
Andrey Andreev4921fed2012-01-07 01:28:07 +0200212 $str = str_replace($matches[0][$i], $out, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200213 }
214 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000215
Barry Mienydd671972010-10-04 16:33:58 +0200216 if ($all)
217 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200218 return str_replace(array('&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#45;'),
219 array('&', '<', '>', '"', "'", '-'),
220 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200221 }
222
223 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
225}
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227// ------------------------------------------------------------------------
228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229if ( ! function_exists('word_censor'))
230{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400231 /**
232 * Word Censoring Function
233 *
234 * Supply a string and an array of disallowed words and any
235 * matched words will be converted to #### or to the replacement
236 * word you've submitted.
237 *
238 * @param string the text string
239 * @param string the array of censoered words
240 * @param string the optional replacement value
241 * @return string
242 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 function word_censor($str, $censored, $replacement = '')
244 {
245 if ( ! is_array($censored))
246 {
247 return $str;
248 }
Barry Mienydd671972010-10-04 16:33:58 +0200249
250 $str = ' '.$str.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000251
Derek Jonesf1b721a2009-01-21 17:52:13 +0000252 // \w, \b and a few others do not match on a unicode character
253 // set for performance reasons. As a result words like über
254 // will not match on a word boundary. Instead, we'll assume that
Derek Jonesdaf9c012010-03-05 10:29:30 -0600255 // a bad word will be bookeneded by any of these characters.
Derek Jonesf1b721a2009-01-21 17:52:13 +0000256 $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 foreach ($censored as $badword)
259 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100260 if ($replacement !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000262 $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/i", "\\1{$replacement}\\3", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
264 else
265 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000266 $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 +0000267 }
268 }
Derek Jonesf1b721a2009-01-21 17:52:13 +0000269
Barry Mienydd671972010-10-04 16:33:58 +0200270 return trim($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
272}
Barry Mienydd671972010-10-04 16:33:58 +0200273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274// ------------------------------------------------------------------------
275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276if ( ! function_exists('highlight_code'))
277{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400278 /**
279 * Code Highlighter
280 *
281 * Colorizes code strings
282 *
283 * @param string the text string
284 * @return string
285 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 function highlight_code($str)
Barry Mienydd671972010-10-04 16:33:58 +0200287 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200288 /* The highlight string function encodes and highlights
289 * brackets so we need them to start raw.
290 *
291 * Also replace any existing PHP tags to temporary markers
292 * so they don't accidentally break the string out of PHP,
293 * and thus, thwart the highlighting.
294 */
295 $str = str_replace(array('&lt;', '&gt;', '<?', '?>', '<%', '%>', '\\', '</script>'),
296 array('<', '>', 'phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
Andrey Andreev8edf87d2012-03-01 20:20:03 +0200297 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000298
299 // The highlight_string function requires that the text be surrounded
300 // by PHP tags, which we will remove later
Andrey Andreev4921fed2012-01-07 01:28:07 +0200301 $str = highlight_string('<?php '.$str.' ?>', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000302
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 // Remove our artificially added PHP, and the syntax highlighting that came with it
Andrey Andreev4921fed2012-01-07 01:28:07 +0200304 $str = preg_replace(array(
305 '/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i',
306 '/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is',
307 '/<span style="color: #[A-Z0-9]+"\><\/span>/i'
308 ),
309 array(
310 '<span style="color: #$1">',
311 "$1</span>\n</span>\n</code>",
312 ''
313 ),
314 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 // Replace our markers back to PHP tags.
Andrey Andreev8edf87d2012-03-01 20:20:03 +0200317 return str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
318 array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'),
319 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 }
321}
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323// ------------------------------------------------------------------------
324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325if ( ! function_exists('highlight_phrase'))
326{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400327 /**
328 * Phrase Highlighter
329 *
330 * Highlights a phrase within a text string
331 *
332 * @param string the text string
333 * @param string the phrase you'd like to highlight
334 * @param string the openging tag to precede the phrase with
335 * @param string the closing tag to end the phrase with
336 * @return string
337 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
339 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100340 if ($str === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
342 return '';
343 }
Barry Mienydd671972010-10-04 16:33:58 +0200344
Alex Bilbie773ccc32012-06-02 11:11:08 +0100345 if ($phrase !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
Andrey Andreevcb324bd2012-01-08 07:06:35 +0200347 return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open.'\\1'.$tag_close, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
349
350 return $str;
351 }
352}
Derek Jonesdaf9c012010-03-05 10:29:30 -0600353
354// ------------------------------------------------------------------------
355
Derek Jonesdaf9c012010-03-05 10:29:30 -0600356if ( ! function_exists('convert_accented_characters'))
357{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400358 /**
359 * Convert Accented Foreign Characters to ASCII
360 *
361 * @param string the text string
362 * @return string
363 */
Eric Barnes5e044802011-01-11 16:10:26 -0500364 function convert_accented_characters($str)
Derek Jonesdaf9c012010-03-05 10:29:30 -0600365 {
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200366 global $foreign_characters;
Barry Mienydd671972010-10-04 16:33:58 +0200367
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200368 if ( ! isset($foreign_characters) OR ! is_array($foreign_characters))
Derek Jonesdaf9c012010-03-05 10:29:30 -0600369 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200370 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200371 {
372 include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
373 }
374 elseif (is_file(APPPATH.'config/foreign_chars.php'))
375 {
376 include(APPPATH.'config/foreign_chars.php');
377 }
378
Andrey Andreev963c96c2012-05-02 13:09:57 +0300379 if ( ! isset($foreign_characters) OR ! is_array($foreign_characters))
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200380 {
381 return $str;
382 }
Derek Jonesdaf9c012010-03-05 10:29:30 -0600383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Eric Barnes5e044802011-01-11 16:10:26 -0500385 return preg_replace(array_keys($foreign_characters), array_values($foreign_characters), $str);
Derek Jonesdaf9c012010-03-05 10:29:30 -0600386 }
387}
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389// ------------------------------------------------------------------------
390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391if ( ! function_exists('word_wrap'))
392{
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300393 /**
394 * Word Wrap
395 *
396 * Wraps text at the specified character. Maintains the integrity of words.
397 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
398 * will URLs.
399 *
400 * @param string $str the text string
401 * @param int $charlim = 76 the number of characters to wrap at
402 * @return string
403 */
Andrey Andreevfc443552012-01-07 02:14:55 +0200404 function word_wrap($str, $charlim = 76)
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200406 // Set the character limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 if ( ! is_numeric($charlim))
Andrey Andreev4921fed2012-01-07 01:28:07 +0200408 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 $charlim = 76;
Andrey Andreev4921fed2012-01-07 01:28:07 +0200410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 // Reduce multiple spaces
Andrey Andreev4921fed2012-01-07 01:28:07 +0200413 $str = preg_replace('| +|', ' ', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 // Standardize newlines
416 if (strpos($str, "\r") !== FALSE)
417 {
Barry Mienydd671972010-10-04 16:33:58 +0200418 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
421 // If the current word is surrounded by {unwrap} tags we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 // strip the entire chunk and replace it with a marker.
423 $unwrap = array();
Andrey Andreev4921fed2012-01-07 01:28:07 +0200424 if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200426 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200428 $unwrap[] = $matches[1][$i];
429 $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 }
431 }
Barry Mienydd671972010-10-04 16:33:58 +0200432
433 // Use PHP's native function to do the initial wordwrap.
434 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev4921fed2012-01-07 01:28:07 +0200435 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 $str = wordwrap($str, $charlim, "\n", FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 // Split the string into individual lines of text and cycle through them
Andrey Andreev4921fed2012-01-07 01:28:07 +0200439 $output = '';
Barry Mienydd671972010-10-04 16:33:58 +0200440 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
442 // Is the line within the allowed character count?
443 // If so we'll join it to the output and continue
444 if (strlen($line) <= $charlim)
445 {
Barry Mienydd671972010-10-04 16:33:58 +0200446 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 continue;
448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 $temp = '';
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500451 while ((strlen($line)) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
453 // If the over-length word is a URL we won't wrap it
Andrey Andreev4921fed2012-01-07 01:28:07 +0200454 if (preg_match('!\[url.+\]|://|wwww.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
456 break;
457 }
458
459 // Trim the word down
Andrey Andreev75124a52012-03-13 12:47:58 +0200460 $temp .= substr($line, 0, $charlim - 1);
461 $line = substr($line, $charlim - 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
464 // If $temp contains data it means we had to split up an over-length
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 // word into smaller chunks so we'll add it back to our current line
Alex Bilbie773ccc32012-06-02 11:11:08 +0100466 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200468 $output .= $temp."\n".$line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
470 else
471 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200472 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
475
476 // Put our markers back
477 if (count($unwrap) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200478 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 foreach ($unwrap as $key => $val)
480 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200481 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
483 }
484
Andrey Andreev4921fed2012-01-07 01:28:07 +0200485 // Remove the unwrap tags and return
486 return str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488}
Barry Mienydd671972010-10-04 16:33:58 +0200489
Greg Akercbe32472010-08-05 14:09:20 -0500490// ------------------------------------------------------------------------
491
492if ( ! function_exists('ellipsize'))
493{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400494 /**
495 * Ellipsize String
496 *
497 * This function will strip tags from a string, split it at its max_length and ellipsize
498 *
499 * @param string string to ellipsize
500 * @param int max length of string
501 * @param mixed int (1|0) or float, .5, .2, etc for position to split
502 * @param string ellipsis ; Default '...'
503 * @return string ellipsized string
504 */
Greg Akercbe32472010-08-05 14:09:20 -0500505 function ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
506 {
507 // Strip tags
508 $str = trim(strip_tags($str));
509
510 // Is the string long enough to ellipsize?
511 if (strlen($str) <= $max_length)
512 {
513 return $str;
514 }
515
516 $beg = substr($str, 0, floor($max_length * $position));
Greg Akercbe32472010-08-05 14:09:20 -0500517 $position = ($position > 1) ? 1 : $position;
518
519 if ($position === 1)
520 {
521 $end = substr($str, 0, -($max_length - strlen($beg)));
522 }
523 else
524 {
525 $end = substr($str, -($max_length - strlen($beg)));
526 }
527
Barry Mienydd671972010-10-04 16:33:58 +0200528 return $beg.$ellipsis.$end;
Greg Akercbe32472010-08-05 14:09:20 -0500529 }
530}
Derek Allard2067d1a2008-11-13 22:59:24 +0000531
532/* End of file text_helper.php */
Andrey Andreeve684bda2012-03-26 13:47:29 +0300533/* Location: ./system/helpers/text_helper.php */