blob: 79aaf1492db92ba0634bc0959693f5c7c994de77 [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 Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, 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 Andreev125ef472016-01-11 12:33:00 +020032 * @copyright Copyright (c) 2014 - 2016, 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+
105 $str = preg_replace('/ {2,}/', ' ', str_replace(array("\r", "\n", "\t", "\x0B", "\x0C"), ' ', $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 Andreev40235e62014-01-09 14:20:57 +0200141 for ($i = 0, $s = strlen($str) - 1, $count = 1, $temp = array(); $i <= $s; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200142 {
143 $ordinal = ord($str[$i]);
144
145 if ($ordinal < 128)
146 {
Derek Jones1978e122009-02-03 14:54:43 +0000147 /*
148 If the $temp array has a value but we have moved on, then it seems only
149 fair that we output that entity and restart $temp before continuing. -Paul
150 */
Andrey Andreev4921fed2012-01-07 01:28:07 +0200151 if (count($temp) === 1)
Derek Jones1978e122009-02-03 14:54:43 +0000152 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200153 $out .= '&#'.array_shift($temp).';';
Derek Jones1978e122009-02-03 14:54:43 +0000154 $count = 1;
155 }
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Jones1978e122009-02-03 14:54:43 +0000157 $out .= $str[$i];
Barry Mienydd671972010-10-04 16:33:58 +0200158 }
159 else
160 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200161 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200162 {
163 $count = ($ordinal < 224) ? 2 : 3;
164 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000165
Barry Mienydd671972010-10-04 16:33:58 +0200166 $temp[] = $ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000167
Andrey Andreev4921fed2012-01-07 01:28:07 +0200168 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200169 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200170 $number = ($count === 3)
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200171 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
172 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
Barry Mienydd671972010-10-04 16:33:58 +0200173
174 $out .= '&#'.$number.';';
175 $count = 1;
176 $temp = array();
177 }
Andrey Andreev40235e62014-01-09 14:20:57 +0200178 // If this is the last iteration, just output whatever we have
179 elseif ($i === $s)
180 {
181 $out .= '&#'.implode(';', $temp).';';
182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183 }
184 }
185
186 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
188}
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190// ------------------------------------------------------------------------
191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192if ( ! function_exists('entities_to_ascii'))
193{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400194 /**
195 * Entities to ASCII
196 *
197 * Converts character entities back to ASCII
198 *
199 * @param string
200 * @param bool
201 * @return string
202 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 function entities_to_ascii($str, $all = TRUE)
204 {
Barry Mienydd671972010-10-04 16:33:58 +0200205 if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
206 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200207 for ($i = 0, $s = count($matches[0]); $i < $s; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200208 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200209 $digits = $matches[1][$i];
Barry Mienydd671972010-10-04 16:33:58 +0200210 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000211
Barry Mienydd671972010-10-04 16:33:58 +0200212 if ($digits < 128)
213 {
214 $out .= chr($digits);
Derek Allard2067d1a2008-11-13 22:59:24 +0000215
Barry Mienydd671972010-10-04 16:33:58 +0200216 }
217 elseif ($digits < 2048)
218 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200219 $out .= chr(192 + (($digits - ($digits % 64)) / 64)).chr(128 + ($digits % 64));
Barry Mienydd671972010-10-04 16:33:58 +0200220 }
221 else
222 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200223 $out .= chr(224 + (($digits - ($digits % 4096)) / 4096))
224 .chr(128 + ((($digits % 4096) - ($digits % 64)) / 64))
225 .chr(128 + ($digits % 64));
Barry Mienydd671972010-10-04 16:33:58 +0200226 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000227
Andrey Andreev4921fed2012-01-07 01:28:07 +0200228 $str = str_replace($matches[0][$i], $out, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200229 }
230 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000231
Barry Mienydd671972010-10-04 16:33:58 +0200232 if ($all)
233 {
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200234 return str_replace(
235 array('&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#45;'),
236 array('&', '<', '>', '"', "'", '-'),
237 $str
238 );
Barry Mienydd671972010-10-04 16:33:58 +0200239 }
240
241 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
243}
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245// ------------------------------------------------------------------------
246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247if ( ! function_exists('word_censor'))
248{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400249 /**
250 * Word Censoring Function
251 *
252 * Supply a string and an array of disallowed words and any
253 * matched words will be converted to #### or to the replacement
254 * word you've submitted.
255 *
256 * @param string the text string
Calvin Tam55bc5052015-07-24 02:27:24 -0700257 * @param string the array of censored words
Timothy Warrenb75faa12012-04-27 12:03:32 -0400258 * @param string the optional replacement value
259 * @return string
260 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 function word_censor($str, $censored, $replacement = '')
262 {
263 if ( ! is_array($censored))
264 {
265 return $str;
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
268 $str = ' '.$str.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000269
Derek Jonesf1b721a2009-01-21 17:52:13 +0000270 // \w, \b and a few others do not match on a unicode character
271 // set for performance reasons. As a result words like über
272 // will not match on a word boundary. Instead, we'll assume that
Derek Jonesdaf9c012010-03-05 10:29:30 -0600273 // a bad word will be bookeneded by any of these characters.
Derek Jonesf1b721a2009-01-21 17:52:13 +0000274 $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 foreach ($censored as $badword)
277 {
Andrey Andreev6af9dd62016-01-29 13:29:57 +0200278 $badword = str_replace('\*', '\w*?', preg_quote($badword, '/'));
Alex Bilbie773ccc32012-06-02 11:11:08 +0100279 if ($replacement !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
Andrey Andreev6af9dd62016-01-29 13:29:57 +0200281 $str = preg_replace(
282 "/({$delim})(".$badword.")({$delim})/i",
283 "\\1{$replacement}\\3",
284 $str
285 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
Andrey Andreev6af9dd62016-01-29 13:29:57 +0200287 elseif (preg_match_all("/{$delim}(".$badword."){$delim}/i", $str, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Andrey Andreev6af9dd62016-01-29 13:29:57 +0200289 $matches = $matches[1];
290 for ($i = count($matches); $i >= 0; $i--)
291 {
292 $length = strlen($matches[$i][0]);
293 $str = substr_replace(
294 $str,
295 str_repeat('#', $length),
296 $matches[$i][1],
297 $length
298 );
299 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
301 }
Derek Jonesf1b721a2009-01-21 17:52:13 +0000302
Barry Mienydd671972010-10-04 16:33:58 +0200303 return trim($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 }
305}
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307// ------------------------------------------------------------------------
308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309if ( ! function_exists('highlight_code'))
310{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400311 /**
312 * Code Highlighter
313 *
314 * Colorizes code strings
315 *
316 * @param string the text string
317 * @return string
318 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 function highlight_code($str)
Barry Mienydd671972010-10-04 16:33:58 +0200320 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200321 /* The highlight string function encodes and highlights
322 * brackets so we need them to start raw.
323 *
324 * Also replace any existing PHP tags to temporary markers
325 * so they don't accidentally break the string out of PHP,
326 * and thus, thwart the highlighting.
327 */
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200328 $str = str_replace(
329 array('&lt;', '&gt;', '<?', '?>', '<%', '%>', '\\', '</script>'),
330 array('<', '>', 'phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
331 $str
332 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000333
334 // The highlight_string function requires that the text be surrounded
335 // by PHP tags, which we will remove later
Andrey Andreev4921fed2012-01-07 01:28:07 +0200336 $str = highlight_string('<?php '.$str.' ?>', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // Remove our artificially added PHP, and the syntax highlighting that came with it
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200339 $str = preg_replace(
340 array(
341 '/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i',
342 '/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is',
343 '/<span style="color: #[A-Z0-9]+"\><\/span>/i'
344 ),
345 array(
346 '<span style="color: #$1">',
347 "$1</span>\n</span>\n</code>",
348 ''
349 ),
350 $str
351 );
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 // Replace our markers back to PHP tags.
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200354 return str_replace(
355 array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
356 array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'),
357 $str
358 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
360}
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362// ------------------------------------------------------------------------
363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364if ( ! function_exists('highlight_phrase'))
365{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400366 /**
367 * Phrase Highlighter
368 *
369 * Highlights a phrase within a text string
370 *
Andrey Andreevac023e12014-01-07 16:13:03 +0200371 * @param string $str the text string
372 * @param string $phrase the phrase you'd like to highlight
373 * @param string $tag_open the openging tag to precede the phrase with
374 * @param string $tag_close the closing tag to end the phrase with
Timothy Warrenb75faa12012-04-27 12:03:32 -0400375 * @return string
376 */
Andrey Andreevac023e12014-01-07 16:13:03 +0200377 function highlight_phrase($str, $phrase, $tag_open = '<mark>', $tag_close = '</mark>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
Andrey Andreevac023e12014-01-07 16:13:03 +0200379 return ($str !== '' && $phrase !== '')
Ivan Tcholakov8bc73eb2014-03-17 05:18:45 +0200380 ? preg_replace('/('.preg_quote($phrase, '/').')/i'.(UTF8_ENABLED ? 'u' : ''), $tag_open.'\\1'.$tag_close, $str)
Andrey Andreevac023e12014-01-07 16:13:03 +0200381 : $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 }
383}
Derek Jonesdaf9c012010-03-05 10:29:30 -0600384
385// ------------------------------------------------------------------------
386
Derek Jonesdaf9c012010-03-05 10:29:30 -0600387if ( ! function_exists('convert_accented_characters'))
388{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400389 /**
390 * Convert Accented Foreign Characters to ASCII
391 *
Andrey Andreev06879112013-01-29 15:05:02 +0200392 * @param string $str Input string
Timothy Warrenb75faa12012-04-27 12:03:32 -0400393 * @return string
394 */
Eric Barnes5e044802011-01-11 16:10:26 -0500395 function convert_accented_characters($str)
Derek Jonesdaf9c012010-03-05 10:29:30 -0600396 {
vlakofff5b4f6a2013-02-28 22:17:51 +0100397 static $array_from, $array_to;
Barry Mienydd671972010-10-04 16:33:58 +0200398
vlakofff5b4f6a2013-02-28 22:17:51 +0100399 if ( ! is_array($array_from))
Derek Jonesdaf9c012010-03-05 10:29:30 -0600400 {
Andrey Andreev06879112013-01-29 15:05:02 +0200401 if (file_exists(APPPATH.'config/foreign_chars.php'))
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200402 {
403 include(APPPATH.'config/foreign_chars.php');
404 }
405
Andrey Andreev06879112013-01-29 15:05:02 +0200406 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200407 {
Andrey Andreev06879112013-01-29 15:05:02 +0200408 include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
409 }
410
411 if (empty($foreign_characters) OR ! is_array($foreign_characters))
412 {
vlakofff5b4f6a2013-02-28 22:17:51 +0100413 $array_from = array();
414 $array_to = array();
415
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200416 return $str;
417 }
Andrey Andreev06879112013-01-29 15:05:02 +0200418
vlakofff5b4f6a2013-02-28 22:17:51 +0100419 $array_from = array_keys($foreign_characters);
420 $array_to = array_values($foreign_characters);
Derek Jonesdaf9c012010-03-05 10:29:30 -0600421 }
Barry Mienydd671972010-10-04 16:33:58 +0200422
vlakofff5b4f6a2013-02-28 22:17:51 +0100423 return preg_replace($array_from, $array_to, $str);
Derek Jonesdaf9c012010-03-05 10:29:30 -0600424 }
425}
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427// ------------------------------------------------------------------------
428
Derek Allard2067d1a2008-11-13 22:59:24 +0000429if ( ! function_exists('word_wrap'))
430{
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300431 /**
432 * Word Wrap
433 *
434 * Wraps text at the specified character. Maintains the integrity of words.
435 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
436 * will URLs.
437 *
438 * @param string $str the text string
439 * @param int $charlim = 76 the number of characters to wrap at
440 * @return string
441 */
Andrey Andreevfc443552012-01-07 02:14:55 +0200442 function word_wrap($str, $charlim = 76)
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200444 // Set the character limit
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200445 is_numeric($charlim) OR $charlim = 76;
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 // Reduce multiple spaces
Andrey Andreev4921fed2012-01-07 01:28:07 +0200448 $str = preg_replace('| +|', ' ', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 // Standardize newlines
451 if (strpos($str, "\r") !== FALSE)
452 {
Barry Mienydd671972010-10-04 16:33:58 +0200453 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
456 // If the current word is surrounded by {unwrap} tags we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 // strip the entire chunk and replace it with a marker.
458 $unwrap = array();
vlakoff0f7eba22014-05-20 10:32:44 +0200459 if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200461 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200463 $unwrap[] = $matches[1][$i];
vlakoff0f7eba22014-05-20 10:32:44 +0200464 $str = str_replace($matches[0][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
466 }
Barry Mienydd671972010-10-04 16:33:58 +0200467
468 // Use PHP's native function to do the initial wordwrap.
469 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev4921fed2012-01-07 01:28:07 +0200470 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 $str = wordwrap($str, $charlim, "\n", FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 // Split the string into individual lines of text and cycle through them
Andrey Andreev4921fed2012-01-07 01:28:07 +0200474 $output = '';
Barry Mienydd671972010-10-04 16:33:58 +0200475 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 {
477 // Is the line within the allowed character count?
478 // If so we'll join it to the output and continue
Andrey Andreev6ce47462014-02-13 03:28:00 +0200479 if (mb_strlen($line) <= $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 {
Barry Mienydd671972010-10-04 16:33:58 +0200481 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 continue;
483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 $temp = '';
Andrey Andreev6ce47462014-02-13 03:28:00 +0200486 while (mb_strlen($line) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
488 // If the over-length word is a URL we won't wrap it
vlakoff2a8560c2014-05-20 10:32:35 +0200489 if (preg_match('!\[url.+\]|://|www\.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
491 break;
492 }
493
494 // Trim the word down
Andrey Andreev6ce47462014-02-13 03:28:00 +0200495 $temp .= mb_substr($line, 0, $charlim - 1);
496 $line = mb_substr($line, $charlim - 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
499 // If $temp contains data it means we had to split up an over-length
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 // word into smaller chunks so we'll add it back to our current line
Alex Bilbie773ccc32012-06-02 11:11:08 +0100501 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200503 $output .= $temp."\n".$line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
505 else
506 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200507 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 }
510
511 // Put our markers back
512 if (count($unwrap) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200513 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 foreach ($unwrap as $key => $val)
515 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200516 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 }
518 }
519
vlakoff0f7eba22014-05-20 10:32:44 +0200520 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 }
522}
Barry Mienydd671972010-10-04 16:33:58 +0200523
Greg Akercbe32472010-08-05 14:09:20 -0500524// ------------------------------------------------------------------------
525
526if ( ! function_exists('ellipsize'))
527{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400528 /**
529 * Ellipsize String
530 *
531 * This function will strip tags from a string, split it at its max_length and ellipsize
532 *
533 * @param string string to ellipsize
534 * @param int max length of string
535 * @param mixed int (1|0) or float, .5, .2, etc for position to split
536 * @param string ellipsis ; Default '...'
537 * @return string ellipsized string
538 */
Greg Akercbe32472010-08-05 14:09:20 -0500539 function ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
540 {
541 // Strip tags
542 $str = trim(strip_tags($str));
543
544 // Is the string long enough to ellipsize?
Andrey Andreev6ce47462014-02-13 03:28:00 +0200545 if (mb_strlen($str) <= $max_length)
Greg Akercbe32472010-08-05 14:09:20 -0500546 {
547 return $str;
548 }
549
Andrey Andreev6ce47462014-02-13 03:28:00 +0200550 $beg = mb_substr($str, 0, floor($max_length * $position));
Greg Akercbe32472010-08-05 14:09:20 -0500551 $position = ($position > 1) ? 1 : $position;
552
553 if ($position === 1)
554 {
Andrey Andreev6ce47462014-02-13 03:28:00 +0200555 $end = mb_substr($str, 0, -($max_length - mb_strlen($beg)));
Greg Akercbe32472010-08-05 14:09:20 -0500556 }
557 else
558 {
Andrey Andreev6ce47462014-02-13 03:28:00 +0200559 $end = mb_substr($str, -($max_length - mb_strlen($beg)));
Greg Akercbe32472010-08-05 14:09:20 -0500560 }
561
Barry Mienydd671972010-10-04 16:33:58 +0200562 return $beg.$ellipsis.$end;
Greg Akercbe32472010-08-05 14:09:20 -0500563 }
564}