blob: 3e2dca8a2fe7c033d5daa91cf6af683697d82390 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreeve684bda2012-03-26 13:47:29 +03008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreeve684bda2012-03-26 13:47:29 +030010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * CodeIgniter Text Helpers
42 *
43 * @package CodeIgniter
44 * @subpackage Helpers
45 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/helpers/text_helper.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49
50// ------------------------------------------------------------------------
51
Derek Allard2067d1a2008-11-13 22:59:24 +000052if ( ! function_exists('word_limiter'))
53{
Timothy Warrenb75faa12012-04-27 12:03:32 -040054 /**
55 * Word Limiter
56 *
57 * Limits a string to X number of words.
58 *
59 * @param string
60 * @param int
61 * @param string the end character. Usually an ellipsis
62 * @return string
63 */
Derek Allard2067d1a2008-11-13 22:59:24 +000064 function word_limiter($str, $limit = 100, $end_char = '&#8230;')
65 {
Alex Bilbie773ccc32012-06-02 11:11:08 +010066 if (trim($str) === '')
Derek Allard2067d1a2008-11-13 22:59:24 +000067 {
68 return $str;
69 }
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071 preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
Barry Mienydd671972010-10-04 16:33:58 +020072
Andrey Andreev4921fed2012-01-07 01:28:07 +020073 if (strlen($str) === strlen($matches[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +000074 {
75 $end_char = '';
76 }
Barry Mienydd671972010-10-04 16:33:58 +020077
Derek Allard2067d1a2008-11-13 22:59:24 +000078 return rtrim($matches[0]).$end_char;
79 }
80}
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082// ------------------------------------------------------------------------
83
Derek Allard2067d1a2008-11-13 22:59:24 +000084if ( ! function_exists('character_limiter'))
85{
Timothy Warrenb75faa12012-04-27 12:03:32 -040086 /**
87 * Character Limiter
88 *
89 * Limits the string based on the character count. Preserves complete words
90 * so the character count may not be exactly as specified.
91 *
92 * @param string
93 * @param int
94 * @param string the end character. Usually an ellipsis
95 * @return string
96 */
Derek Allard2067d1a2008-11-13 22:59:24 +000097 function character_limiter($str, $n = 500, $end_char = '&#8230;')
98 {
Mohammad Javad Naderic6f5ed12013-08-22 18:33:50 +043099 if (mb_strlen($str) < $n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
101 return $str;
102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
vlakoff62a5ee32012-09-04 23:12:49 +0200104 // a bit complicated, but faster than preg_replace with \s+
Andrey Andreev2b26ccf2016-12-16 12:02:00 +0200105 $str = preg_replace('/ {2,}/', ' ', str_replace(array("\r", "\n", "\t", "\v", "\f"), ' ', $str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000106
Mohammad Javad Naderic6f5ed12013-08-22 18:33:50 +0430107 if (mb_strlen($str) <= $n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
109 return $str;
110 }
Barry Mienydd671972010-10-04 16:33:58 +0200111
Andrey Andreev4921fed2012-01-07 01:28:07 +0200112 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 foreach (explode(' ', trim($str)) as $val)
114 {
Derek Jones01d6b4f2009-02-03 14:51:00 +0000115 $out .= $val.' ';
Barry Mienydd671972010-10-04 16:33:58 +0200116
Mohammad Javad Naderic6f5ed12013-08-22 18:33:50 +0430117 if (mb_strlen($out) >= $n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
Derek Jones01d6b4f2009-02-03 14:51:00 +0000119 $out = trim($out);
Mohammad Javad Naderic6f5ed12013-08-22 18:33:50 +0430120 return (mb_strlen($out) === mb_strlen($str)) ? $out : $out.$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200121 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 }
123 }
124}
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126// ------------------------------------------------------------------------
127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128if ( ! function_exists('ascii_to_entities'))
129{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400130 /**
131 * High ASCII to Entities
132 *
Andrey Andreev7d753462012-10-27 03:37:40 +0300133 * Converts high ASCII text and MS Word special characters to character entities
Timothy Warrenb75faa12012-04-27 12:03:32 -0400134 *
Andrey Andreev7d753462012-10-27 03:37:40 +0300135 * @param string $str
Timothy Warrenb75faa12012-04-27 12:03:32 -0400136 * @return string
137 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 function ascii_to_entities($str)
139 {
Andrey Andreev7d753462012-10-27 03:37:40 +0300140 $out = '';
Andrey Andreevf5652122017-01-19 15:17:00 +0200141 $length = defined('MB_OVERLOAD_STRING')
142 ? mb_strlen($str, '8bit') - 1
143 : strlen($str) - 1;
144 for ($i = 0, $count = 1, $temp = array(); $i <= $length; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200145 {
146 $ordinal = ord($str[$i]);
147
148 if ($ordinal < 128)
149 {
Derek Jones1978e122009-02-03 14:54:43 +0000150 /*
151 If the $temp array has a value but we have moved on, then it seems only
152 fair that we output that entity and restart $temp before continuing. -Paul
153 */
Andrey Andreev4921fed2012-01-07 01:28:07 +0200154 if (count($temp) === 1)
Derek Jones1978e122009-02-03 14:54:43 +0000155 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200156 $out .= '&#'.array_shift($temp).';';
Derek Jones1978e122009-02-03 14:54:43 +0000157 $count = 1;
158 }
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Jones1978e122009-02-03 14:54:43 +0000160 $out .= $str[$i];
Barry Mienydd671972010-10-04 16:33:58 +0200161 }
162 else
163 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200164 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200165 {
166 $count = ($ordinal < 224) ? 2 : 3;
167 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000168
Barry Mienydd671972010-10-04 16:33:58 +0200169 $temp[] = $ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000170
Andrey Andreev4921fed2012-01-07 01:28:07 +0200171 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200172 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200173 $number = ($count === 3)
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200174 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
175 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
Barry Mienydd671972010-10-04 16:33:58 +0200176
177 $out .= '&#'.$number.';';
178 $count = 1;
179 $temp = array();
180 }
Andrey Andreev40235e62014-01-09 14:20:57 +0200181 // If this is the last iteration, just output whatever we have
Andrey Andreevf5652122017-01-19 15:17:00 +0200182 elseif ($i === $length)
Andrey Andreev40235e62014-01-09 14:20:57 +0200183 {
184 $out .= '&#'.implode(';', $temp).';';
185 }
Barry Mienydd671972010-10-04 16:33:58 +0200186 }
187 }
188
189 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
191}
Barry Mienydd671972010-10-04 16:33:58 +0200192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193// ------------------------------------------------------------------------
194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195if ( ! function_exists('entities_to_ascii'))
196{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400197 /**
198 * Entities to ASCII
199 *
200 * Converts character entities back to ASCII
201 *
202 * @param string
203 * @param bool
204 * @return string
205 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 function entities_to_ascii($str, $all = TRUE)
207 {
Barry Mienydd671972010-10-04 16:33:58 +0200208 if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
209 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200210 for ($i = 0, $s = count($matches[0]); $i < $s; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200211 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200212 $digits = $matches[1][$i];
Barry Mienydd671972010-10-04 16:33:58 +0200213 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000214
Barry Mienydd671972010-10-04 16:33:58 +0200215 if ($digits < 128)
216 {
217 $out .= chr($digits);
Derek Allard2067d1a2008-11-13 22:59:24 +0000218
Barry Mienydd671972010-10-04 16:33:58 +0200219 }
220 elseif ($digits < 2048)
221 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200222 $out .= chr(192 + (($digits - ($digits % 64)) / 64)).chr(128 + ($digits % 64));
Barry Mienydd671972010-10-04 16:33:58 +0200223 }
224 else
225 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200226 $out .= chr(224 + (($digits - ($digits % 4096)) / 4096))
227 .chr(128 + ((($digits % 4096) - ($digits % 64)) / 64))
228 .chr(128 + ($digits % 64));
Barry Mienydd671972010-10-04 16:33:58 +0200229 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000230
Andrey Andreev4921fed2012-01-07 01:28:07 +0200231 $str = str_replace($matches[0][$i], $out, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200232 }
233 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000234
Barry Mienydd671972010-10-04 16:33:58 +0200235 if ($all)
236 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200237 return str_replace(
238 array('&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#45;'),
239 array('&', '<', '>', '"', "'", '-'),
240 $str
241 );
Barry Mienydd671972010-10-04 16:33:58 +0200242 }
243
244 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
246}
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248// ------------------------------------------------------------------------
249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250if ( ! function_exists('word_censor'))
251{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400252 /**
253 * Word Censoring Function
254 *
255 * Supply a string and an array of disallowed words and any
256 * matched words will be converted to #### or to the replacement
257 * word you've submitted.
258 *
259 * @param string the text string
Calvin Tam55bc5052015-07-24 02:27:24 -0700260 * @param string the array of censored words
Timothy Warrenb75faa12012-04-27 12:03:32 -0400261 * @param string the optional replacement value
262 * @return string
263 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 function word_censor($str, $censored, $replacement = '')
265 {
266 if ( ! is_array($censored))
267 {
268 return $str;
269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
271 $str = ' '.$str.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000272
Derek Jonesf1b721a2009-01-21 17:52:13 +0000273 // \w, \b and a few others do not match on a unicode character
274 // set for performance reasons. As a result words like über
275 // will not match on a word boundary. Instead, we'll assume that
Derek Jonesdaf9c012010-03-05 10:29:30 -0600276 // a bad word will be bookeneded by any of these characters.
Derek Jonesf1b721a2009-01-21 17:52:13 +0000277 $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 foreach ($censored as $badword)
280 {
Andrey Andreev6af9dd62016-01-29 13:29:57 +0200281 $badword = str_replace('\*', '\w*?', preg_quote($badword, '/'));
Alex Bilbie773ccc32012-06-02 11:11:08 +0100282 if ($replacement !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 {
Andrey Andreev6af9dd62016-01-29 13:29:57 +0200284 $str = preg_replace(
285 "/({$delim})(".$badword.")({$delim})/i",
286 "\\1{$replacement}\\3",
287 $str
288 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
Andrey Andreev6af9dd62016-01-29 13:29:57 +0200290 elseif (preg_match_all("/{$delim}(".$badword."){$delim}/i", $str, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
Andrey Andreev6af9dd62016-01-29 13:29:57 +0200292 $matches = $matches[1];
Andrey Andreev9aab22e2016-01-29 16:19:46 +0200293 for ($i = count($matches) - 1; $i >= 0; $i--)
Andrey Andreev6af9dd62016-01-29 13:29:57 +0200294 {
295 $length = strlen($matches[$i][0]);
296 $str = substr_replace(
297 $str,
298 str_repeat('#', $length),
299 $matches[$i][1],
300 $length
301 );
302 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
304 }
Derek Jonesf1b721a2009-01-21 17:52:13 +0000305
Barry Mienydd671972010-10-04 16:33:58 +0200306 return trim($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
308}
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310// ------------------------------------------------------------------------
311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312if ( ! function_exists('highlight_code'))
313{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400314 /**
315 * Code Highlighter
316 *
317 * Colorizes code strings
318 *
319 * @param string the text string
320 * @return string
321 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 function highlight_code($str)
Barry Mienydd671972010-10-04 16:33:58 +0200323 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200324 /* The highlight string function encodes and highlights
325 * brackets so we need them to start raw.
326 *
327 * Also replace any existing PHP tags to temporary markers
328 * so they don't accidentally break the string out of PHP,
329 * and thus, thwart the highlighting.
330 */
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200331 $str = str_replace(
332 array('&lt;', '&gt;', '<?', '?>', '<%', '%>', '\\', '</script>'),
333 array('<', '>', 'phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
334 $str
335 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000336
337 // The highlight_string function requires that the text be surrounded
338 // by PHP tags, which we will remove later
Andrey Andreev4921fed2012-01-07 01:28:07 +0200339 $str = highlight_string('<?php '.$str.' ?>', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // Remove our artificially added PHP, and the syntax highlighting that came with it
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200342 $str = preg_replace(
343 array(
344 '/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i',
345 '/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is',
346 '/<span style="color: #[A-Z0-9]+"\><\/span>/i'
347 ),
348 array(
349 '<span style="color: #$1">',
350 "$1</span>\n</span>\n</code>",
351 ''
352 ),
353 $str
354 );
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 // Replace our markers back to PHP tags.
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200357 return str_replace(
358 array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
359 array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'),
360 $str
361 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 }
363}
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365// ------------------------------------------------------------------------
366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367if ( ! function_exists('highlight_phrase'))
368{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400369 /**
370 * Phrase Highlighter
371 *
372 * Highlights a phrase within a text string
373 *
Andrey Andreevac023e12014-01-07 16:13:03 +0200374 * @param string $str the text string
375 * @param string $phrase the phrase you'd like to highlight
376 * @param string $tag_open the openging tag to precede the phrase with
377 * @param string $tag_close the closing tag to end the phrase with
Timothy Warrenb75faa12012-04-27 12:03:32 -0400378 * @return string
379 */
Andrey Andreevac023e12014-01-07 16:13:03 +0200380 function highlight_phrase($str, $phrase, $tag_open = '<mark>', $tag_close = '</mark>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
Andrey Andreevac023e12014-01-07 16:13:03 +0200382 return ($str !== '' && $phrase !== '')
Ivan Tcholakov8bc73eb2014-03-17 05:18:45 +0200383 ? preg_replace('/('.preg_quote($phrase, '/').')/i'.(UTF8_ENABLED ? 'u' : ''), $tag_open.'\\1'.$tag_close, $str)
Andrey Andreevac023e12014-01-07 16:13:03 +0200384 : $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
386}
Derek Jonesdaf9c012010-03-05 10:29:30 -0600387
388// ------------------------------------------------------------------------
389
Derek Jonesdaf9c012010-03-05 10:29:30 -0600390if ( ! function_exists('convert_accented_characters'))
391{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400392 /**
393 * Convert Accented Foreign Characters to ASCII
394 *
Andrey Andreev06879112013-01-29 15:05:02 +0200395 * @param string $str Input string
Timothy Warrenb75faa12012-04-27 12:03:32 -0400396 * @return string
397 */
Eric Barnes5e044802011-01-11 16:10:26 -0500398 function convert_accented_characters($str)
Derek Jonesdaf9c012010-03-05 10:29:30 -0600399 {
vlakofff5b4f6a2013-02-28 22:17:51 +0100400 static $array_from, $array_to;
Barry Mienydd671972010-10-04 16:33:58 +0200401
vlakofff5b4f6a2013-02-28 22:17:51 +0100402 if ( ! is_array($array_from))
Derek Jonesdaf9c012010-03-05 10:29:30 -0600403 {
Andrey Andreev06879112013-01-29 15:05:02 +0200404 if (file_exists(APPPATH.'config/foreign_chars.php'))
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200405 {
406 include(APPPATH.'config/foreign_chars.php');
407 }
408
Andrey Andreev06879112013-01-29 15:05:02 +0200409 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200410 {
Andrey Andreev06879112013-01-29 15:05:02 +0200411 include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
412 }
413
414 if (empty($foreign_characters) OR ! is_array($foreign_characters))
415 {
vlakofff5b4f6a2013-02-28 22:17:51 +0100416 $array_from = array();
417 $array_to = array();
418
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200419 return $str;
420 }
Andrey Andreev06879112013-01-29 15:05:02 +0200421
vlakofff5b4f6a2013-02-28 22:17:51 +0100422 $array_from = array_keys($foreign_characters);
423 $array_to = array_values($foreign_characters);
Derek Jonesdaf9c012010-03-05 10:29:30 -0600424 }
Barry Mienydd671972010-10-04 16:33:58 +0200425
vlakofff5b4f6a2013-02-28 22:17:51 +0100426 return preg_replace($array_from, $array_to, $str);
Derek Jonesdaf9c012010-03-05 10:29:30 -0600427 }
428}
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430// ------------------------------------------------------------------------
431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432if ( ! function_exists('word_wrap'))
433{
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300434 /**
435 * Word Wrap
436 *
437 * Wraps text at the specified character. Maintains the integrity of words.
438 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
439 * will URLs.
440 *
441 * @param string $str the text string
442 * @param int $charlim = 76 the number of characters to wrap at
443 * @return string
444 */
Andrey Andreevfc443552012-01-07 02:14:55 +0200445 function word_wrap($str, $charlim = 76)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200447 // Set the character limit
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200448 is_numeric($charlim) OR $charlim = 76;
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 // Reduce multiple spaces
Andrey Andreev4921fed2012-01-07 01:28:07 +0200451 $str = preg_replace('| +|', ' ', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 // Standardize newlines
454 if (strpos($str, "\r") !== FALSE)
455 {
Barry Mienydd671972010-10-04 16:33:58 +0200456 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
459 // If the current word is surrounded by {unwrap} tags we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 // strip the entire chunk and replace it with a marker.
461 $unwrap = array();
vlakoff0f7eba22014-05-20 10:32:44 +0200462 if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200464 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200466 $unwrap[] = $matches[1][$i];
vlakoff0f7eba22014-05-20 10:32:44 +0200467 $str = str_replace($matches[0][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
471 // Use PHP's native function to do the initial wordwrap.
472 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev4921fed2012-01-07 01:28:07 +0200473 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 $str = wordwrap($str, $charlim, "\n", FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 // Split the string into individual lines of text and cycle through them
Andrey Andreev4921fed2012-01-07 01:28:07 +0200477 $output = '';
Barry Mienydd671972010-10-04 16:33:58 +0200478 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
480 // Is the line within the allowed character count?
481 // If so we'll join it to the output and continue
Andrey Andreev6ce47462014-02-13 03:28:00 +0200482 if (mb_strlen($line) <= $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
Barry Mienydd671972010-10-04 16:33:58 +0200484 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 continue;
486 }
Barry Mienydd671972010-10-04 16:33:58 +0200487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 $temp = '';
Andrey Andreev6ce47462014-02-13 03:28:00 +0200489 while (mb_strlen($line) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
491 // If the over-length word is a URL we won't wrap it
vlakoff2a8560c2014-05-20 10:32:35 +0200492 if (preg_match('!\[url.+\]|://|www\.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
494 break;
495 }
496
497 // Trim the word down
Andrey Andreev6ce47462014-02-13 03:28:00 +0200498 $temp .= mb_substr($line, 0, $charlim - 1);
499 $line = mb_substr($line, $charlim - 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
Barry Mienydd671972010-10-04 16:33:58 +0200501
502 // If $temp contains data it means we had to split up an over-length
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 // word into smaller chunks so we'll add it back to our current line
Alex Bilbie773ccc32012-06-02 11:11:08 +0100504 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200506 $output .= $temp."\n".$line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
508 else
509 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200510 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
513
514 // Put our markers back
515 if (count($unwrap) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200516 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 foreach ($unwrap as $key => $val)
518 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200519 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 }
521 }
522
vlakoff0f7eba22014-05-20 10:32:44 +0200523 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
525}
Barry Mienydd671972010-10-04 16:33:58 +0200526
Greg Akercbe32472010-08-05 14:09:20 -0500527// ------------------------------------------------------------------------
528
529if ( ! function_exists('ellipsize'))
530{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400531 /**
532 * Ellipsize String
533 *
534 * This function will strip tags from a string, split it at its max_length and ellipsize
535 *
536 * @param string string to ellipsize
537 * @param int max length of string
538 * @param mixed int (1|0) or float, .5, .2, etc for position to split
539 * @param string ellipsis ; Default '...'
540 * @return string ellipsized string
541 */
Greg Akercbe32472010-08-05 14:09:20 -0500542 function ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
543 {
544 // Strip tags
545 $str = trim(strip_tags($str));
546
547 // Is the string long enough to ellipsize?
Andrey Andreev6ce47462014-02-13 03:28:00 +0200548 if (mb_strlen($str) <= $max_length)
Greg Akercbe32472010-08-05 14:09:20 -0500549 {
550 return $str;
551 }
552
Andrey Andreev6ce47462014-02-13 03:28:00 +0200553 $beg = mb_substr($str, 0, floor($max_length * $position));
Greg Akercbe32472010-08-05 14:09:20 -0500554 $position = ($position > 1) ? 1 : $position;
555
556 if ($position === 1)
557 {
Andrey Andreev6ce47462014-02-13 03:28:00 +0200558 $end = mb_substr($str, 0, -($max_length - mb_strlen($beg)));
Greg Akercbe32472010-08-05 14:09:20 -0500559 }
560 else
561 {
Andrey Andreev6ce47462014-02-13 03:28:00 +0200562 $end = mb_substr($str, -($max_length - mb_strlen($beg)));
Greg Akercbe32472010-08-05 14:09:20 -0500563 }
564
Barry Mienydd671972010-10-04 16:33:58 +0200565 return $beg.$ellipsis.$end;
Greg Akercbe32472010-08-05 14:09:20 -0500566 }
567}