blob: 1100e670d01443f9f5ec62dc346dc4f9f2c55eae [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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://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 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100278 if ($replacement !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000280 $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/i", "\\1{$replacement}\\3", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
282 else
283 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000284 $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 +0000285 }
286 }
Derek Jonesf1b721a2009-01-21 17:52:13 +0000287
Barry Mienydd671972010-10-04 16:33:58 +0200288 return trim($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
290}
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292// ------------------------------------------------------------------------
293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294if ( ! function_exists('highlight_code'))
295{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400296 /**
297 * Code Highlighter
298 *
299 * Colorizes code strings
300 *
301 * @param string the text string
302 * @return string
303 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 function highlight_code($str)
Barry Mienydd671972010-10-04 16:33:58 +0200305 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200306 /* The highlight string function encodes and highlights
307 * brackets so we need them to start raw.
308 *
309 * Also replace any existing PHP tags to temporary markers
310 * so they don't accidentally break the string out of PHP,
311 * and thus, thwart the highlighting.
312 */
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200313 $str = str_replace(
314 array('&lt;', '&gt;', '<?', '?>', '<%', '%>', '\\', '</script>'),
315 array('<', '>', 'phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
316 $str
317 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000318
319 // The highlight_string function requires that the text be surrounded
320 // by PHP tags, which we will remove later
Andrey Andreev4921fed2012-01-07 01:28:07 +0200321 $str = highlight_string('<?php '.$str.' ?>', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // Remove our artificially added PHP, and the syntax highlighting that came with it
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200324 $str = preg_replace(
325 array(
326 '/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i',
327 '/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is',
328 '/<span style="color: #[A-Z0-9]+"\><\/span>/i'
329 ),
330 array(
331 '<span style="color: #$1">',
332 "$1</span>\n</span>\n</code>",
333 ''
334 ),
335 $str
336 );
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // Replace our markers back to PHP tags.
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200339 return str_replace(
340 array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
341 array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'),
342 $str
343 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
345}
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347// ------------------------------------------------------------------------
348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349if ( ! function_exists('highlight_phrase'))
350{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400351 /**
352 * Phrase Highlighter
353 *
354 * Highlights a phrase within a text string
355 *
Andrey Andreevac023e12014-01-07 16:13:03 +0200356 * @param string $str the text string
357 * @param string $phrase the phrase you'd like to highlight
358 * @param string $tag_open the openging tag to precede the phrase with
359 * @param string $tag_close the closing tag to end the phrase with
Timothy Warrenb75faa12012-04-27 12:03:32 -0400360 * @return string
361 */
Andrey Andreevac023e12014-01-07 16:13:03 +0200362 function highlight_phrase($str, $phrase, $tag_open = '<mark>', $tag_close = '</mark>')
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
Andrey Andreevac023e12014-01-07 16:13:03 +0200364 return ($str !== '' && $phrase !== '')
Ivan Tcholakov8bc73eb2014-03-17 05:18:45 +0200365 ? preg_replace('/('.preg_quote($phrase, '/').')/i'.(UTF8_ENABLED ? 'u' : ''), $tag_open.'\\1'.$tag_close, $str)
Andrey Andreevac023e12014-01-07 16:13:03 +0200366 : $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 }
368}
Derek Jonesdaf9c012010-03-05 10:29:30 -0600369
370// ------------------------------------------------------------------------
371
Derek Jonesdaf9c012010-03-05 10:29:30 -0600372if ( ! function_exists('convert_accented_characters'))
373{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400374 /**
375 * Convert Accented Foreign Characters to ASCII
376 *
Andrey Andreev06879112013-01-29 15:05:02 +0200377 * @param string $str Input string
Timothy Warrenb75faa12012-04-27 12:03:32 -0400378 * @return string
379 */
Eric Barnes5e044802011-01-11 16:10:26 -0500380 function convert_accented_characters($str)
Derek Jonesdaf9c012010-03-05 10:29:30 -0600381 {
vlakofff5b4f6a2013-02-28 22:17:51 +0100382 static $array_from, $array_to;
Barry Mienydd671972010-10-04 16:33:58 +0200383
vlakofff5b4f6a2013-02-28 22:17:51 +0100384 if ( ! is_array($array_from))
Derek Jonesdaf9c012010-03-05 10:29:30 -0600385 {
Andrey Andreev06879112013-01-29 15:05:02 +0200386 if (file_exists(APPPATH.'config/foreign_chars.php'))
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200387 {
388 include(APPPATH.'config/foreign_chars.php');
389 }
390
Andrey Andreev06879112013-01-29 15:05:02 +0200391 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200392 {
Andrey Andreev06879112013-01-29 15:05:02 +0200393 include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
394 }
395
396 if (empty($foreign_characters) OR ! is_array($foreign_characters))
397 {
vlakofff5b4f6a2013-02-28 22:17:51 +0100398 $array_from = array();
399 $array_to = array();
400
Andrey Andreev0f2ec5b2012-01-16 14:02:24 +0200401 return $str;
402 }
Andrey Andreev06879112013-01-29 15:05:02 +0200403
vlakofff5b4f6a2013-02-28 22:17:51 +0100404 $array_from = array_keys($foreign_characters);
405 $array_to = array_values($foreign_characters);
Derek Jonesdaf9c012010-03-05 10:29:30 -0600406 }
Barry Mienydd671972010-10-04 16:33:58 +0200407
vlakofff5b4f6a2013-02-28 22:17:51 +0100408 return preg_replace($array_from, $array_to, $str);
Derek Jonesdaf9c012010-03-05 10:29:30 -0600409 }
410}
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412// ------------------------------------------------------------------------
413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414if ( ! function_exists('word_wrap'))
415{
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300416 /**
417 * Word Wrap
418 *
419 * Wraps text at the specified character. Maintains the integrity of words.
420 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
421 * will URLs.
422 *
423 * @param string $str the text string
424 * @param int $charlim = 76 the number of characters to wrap at
425 * @return string
426 */
Andrey Andreevfc443552012-01-07 02:14:55 +0200427 function word_wrap($str, $charlim = 76)
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200429 // Set the character limit
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200430 is_numeric($charlim) OR $charlim = 76;
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 // Reduce multiple spaces
Andrey Andreev4921fed2012-01-07 01:28:07 +0200433 $str = preg_replace('| +|', ' ', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 // Standardize newlines
436 if (strpos($str, "\r") !== FALSE)
437 {
Barry Mienydd671972010-10-04 16:33:58 +0200438 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
441 // If the current word is surrounded by {unwrap} tags we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 // strip the entire chunk and replace it with a marker.
443 $unwrap = array();
vlakoff0f7eba22014-05-20 10:32:44 +0200444 if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches))
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200446 for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200448 $unwrap[] = $matches[1][$i];
vlakoff0f7eba22014-05-20 10:32:44 +0200449 $str = str_replace($matches[0][$i], '{{unwrapped'.$i.'}}', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
453 // Use PHP's native function to do the initial wordwrap.
454 // We set the cut flag to FALSE so that any individual words that are
Andrey Andreev4921fed2012-01-07 01:28:07 +0200455 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 $str = wordwrap($str, $charlim, "\n", FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 // Split the string into individual lines of text and cycle through them
Andrey Andreev4921fed2012-01-07 01:28:07 +0200459 $output = '';
Barry Mienydd671972010-10-04 16:33:58 +0200460 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
462 // Is the line within the allowed character count?
463 // If so we'll join it to the output and continue
Andrey Andreev6ce47462014-02-13 03:28:00 +0200464 if (mb_strlen($line) <= $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
Barry Mienydd671972010-10-04 16:33:58 +0200466 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 continue;
468 }
Barry Mienydd671972010-10-04 16:33:58 +0200469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 $temp = '';
Andrey Andreev6ce47462014-02-13 03:28:00 +0200471 while (mb_strlen($line) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
473 // If the over-length word is a URL we won't wrap it
vlakoff2a8560c2014-05-20 10:32:35 +0200474 if (preg_match('!\[url.+\]|://|www\.!', $line))
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
476 break;
477 }
478
479 // Trim the word down
Andrey Andreev6ce47462014-02-13 03:28:00 +0200480 $temp .= mb_substr($line, 0, $charlim - 1);
481 $line = mb_substr($line, $charlim - 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
484 // If $temp contains data it means we had to split up an over-length
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 // word into smaller chunks so we'll add it back to our current line
Alex Bilbie773ccc32012-06-02 11:11:08 +0100486 if ($temp !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200488 $output .= $temp."\n".$line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
490 else
491 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200492 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
495
496 // Put our markers back
497 if (count($unwrap) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200498 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 foreach ($unwrap as $key => $val)
500 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200501 $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
503 }
504
vlakoff0f7eba22014-05-20 10:32:44 +0200505 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
507}
Barry Mienydd671972010-10-04 16:33:58 +0200508
Greg Akercbe32472010-08-05 14:09:20 -0500509// ------------------------------------------------------------------------
510
511if ( ! function_exists('ellipsize'))
512{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400513 /**
514 * Ellipsize String
515 *
516 * This function will strip tags from a string, split it at its max_length and ellipsize
517 *
518 * @param string string to ellipsize
519 * @param int max length of string
520 * @param mixed int (1|0) or float, .5, .2, etc for position to split
521 * @param string ellipsis ; Default '...'
522 * @return string ellipsized string
523 */
Greg Akercbe32472010-08-05 14:09:20 -0500524 function ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
525 {
526 // Strip tags
527 $str = trim(strip_tags($str));
528
529 // Is the string long enough to ellipsize?
Andrey Andreev6ce47462014-02-13 03:28:00 +0200530 if (mb_strlen($str) <= $max_length)
Greg Akercbe32472010-08-05 14:09:20 -0500531 {
532 return $str;
533 }
534
Andrey Andreev6ce47462014-02-13 03:28:00 +0200535 $beg = mb_substr($str, 0, floor($max_length * $position));
Greg Akercbe32472010-08-05 14:09:20 -0500536 $position = ($position > 1) ? 1 : $position;
537
538 if ($position === 1)
539 {
Andrey Andreev6ce47462014-02-13 03:28:00 +0200540 $end = mb_substr($str, 0, -($max_length - mb_strlen($beg)));
Greg Akercbe32472010-08-05 14:09:20 -0500541 }
542 else
543 {
Andrey Andreev6ce47462014-02-13 03:28:00 +0200544 $end = mb_substr($str, -($max_length - mb_strlen($beg)));
Greg Akercbe32472010-08-05 14:09:20 -0500545 }
546
Barry Mienydd671972010-10-04 16:33:58 +0200547 return $beg.$ellipsis.$end;
Greg Akercbe32472010-08-05 14:09:20 -0500548 }
549}