blob: 2d0f78d2c8e085aeb7ae8808d94bb2a1afa31fd3 [file] [log] [blame]
Andrey Andreeve684bda2012-03-26 13:47:29 +03001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter Text Helpers
32 *
33 * @package CodeIgniter
34 * @subpackage Helpers
35 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/helpers/text_helper.html
38 */
39
40// ------------------------------------------------------------------------
41
42/**
43 * Word Limiter
44 *
45 * Limits a string to X number of words.
46 *
47 * @access public
48 * @param string
49 * @param integer
50 * @param string the end character. Usually an ellipsis
51 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020052 */
Derek Allard2067d1a2008-11-13 22:59:24 +000053if ( ! function_exists('word_limiter'))
54{
55 function word_limiter($str, $limit = 100, $end_char = '&#8230;')
56 {
57 if (trim($str) == '')
58 {
59 return $str;
60 }
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064 if (strlen($str) == strlen($matches[0]))
65 {
66 $end_char = '';
67 }
Barry Mienydd671972010-10-04 16:33:58 +020068
Derek Allard2067d1a2008-11-13 22:59:24 +000069 return rtrim($matches[0]).$end_char;
70 }
71}
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073// ------------------------------------------------------------------------
74
75/**
76 * Character Limiter
77 *
Derek Jones37f4b9c2011-07-01 17:56:50 -050078 * Limits the string based on the character count. Preserves complete words
Derek Allard2067d1a2008-11-13 22:59:24 +000079 * so the character count may not be exactly as specified.
80 *
81 * @access public
82 * @param string
83 * @param integer
84 * @param string the end character. Usually an ellipsis
85 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020086 */
Derek Allard2067d1a2008-11-13 22:59:24 +000087if ( ! function_exists('character_limiter'))
88{
89 function character_limiter($str, $n = 500, $end_char = '&#8230;')
90 {
91 if (strlen($str) < $n)
92 {
93 return $str;
94 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Allard2067d1a2008-11-13 22:59:24 +000096 $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
97
98 if (strlen($str) <= $n)
99 {
100 return $str;
101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 $out = "";
104 foreach (explode(' ', trim($str)) as $val)
105 {
Derek Jones01d6b4f2009-02-03 14:51:00 +0000106 $out .= $val.' ';
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 if (strlen($out) >= $n)
109 {
Derek Jones01d6b4f2009-02-03 14:51:00 +0000110 $out = trim($out);
111 return (strlen($out) == strlen($str)) ? $out : $out.$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200112 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114 }
115}
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117// ------------------------------------------------------------------------
118
119/**
120 * High ASCII to Entities
121 *
122 * Converts High ascii text and MS Word special characters to character entities
123 *
124 * @access public
125 * @param string
126 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200127 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000128if ( ! function_exists('ascii_to_entities'))
129{
130 function ascii_to_entities($str)
131 {
Barry Mienydd671972010-10-04 16:33:58 +0200132 $count = 1;
133 $out = '';
134 $temp = array();
135
136 for ($i = 0, $s = strlen($str); $i < $s; $i++)
137 {
138 $ordinal = ord($str[$i]);
139
140 if ($ordinal < 128)
141 {
Derek Jones1978e122009-02-03 14:54:43 +0000142 /*
143 If the $temp array has a value but we have moved on, then it seems only
144 fair that we output that entity and restart $temp before continuing. -Paul
145 */
146 if (count($temp) == 1)
147 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500148 $out .= '&#'.array_shift($temp).';';
Derek Jones1978e122009-02-03 14:54:43 +0000149 $count = 1;
150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Jones1978e122009-02-03 14:54:43 +0000152 $out .= $str[$i];
Barry Mienydd671972010-10-04 16:33:58 +0200153 }
154 else
155 {
156 if (count($temp) == 0)
157 {
158 $count = ($ordinal < 224) ? 2 : 3;
159 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000160
Barry Mienydd671972010-10-04 16:33:58 +0200161 $temp[] = $ordinal;
Derek Allard2067d1a2008-11-13 22:59:24 +0000162
Barry Mienydd671972010-10-04 16:33:58 +0200163 if (count($temp) == $count)
164 {
165 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
166
167 $out .= '&#'.$number.';';
168 $count = 1;
169 $temp = array();
170 }
171 }
172 }
173
174 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
176}
Barry Mienydd671972010-10-04 16:33:58 +0200177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178// ------------------------------------------------------------------------
179
180/**
181 * Entities to ASCII
182 *
183 * Converts character entities back to ASCII
184 *
185 * @access public
186 * @param string
187 * @param bool
188 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200189 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000190if ( ! function_exists('entities_to_ascii'))
191{
192 function entities_to_ascii($str, $all = TRUE)
193 {
Barry Mienydd671972010-10-04 16:33:58 +0200194 if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
195 {
196 for ($i = 0, $s = count($matches['0']); $i < $s; $i++)
197 {
198 $digits = $matches['1'][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000199
Barry Mienydd671972010-10-04 16:33:58 +0200200 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000201
Barry Mienydd671972010-10-04 16:33:58 +0200202 if ($digits < 128)
203 {
204 $out .= chr($digits);
Derek Allard2067d1a2008-11-13 22:59:24 +0000205
Barry Mienydd671972010-10-04 16:33:58 +0200206 }
207 elseif ($digits < 2048)
208 {
209 $out .= chr(192 + (($digits - ($digits % 64)) / 64));
210 $out .= chr(128 + ($digits % 64));
211 }
212 else
213 {
214 $out .= chr(224 + (($digits - ($digits % 4096)) / 4096));
215 $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64));
216 $out .= chr(128 + ($digits % 64));
217 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000218
Barry Mienydd671972010-10-04 16:33:58 +0200219 $str = str_replace($matches['0'][$i], $out, $str);
220 }
221 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000222
Barry Mienydd671972010-10-04 16:33:58 +0200223 if ($all)
224 {
225 $str = str_replace(array("&amp;", "&lt;", "&gt;", "&quot;", "&apos;", "&#45;"),
226 array("&","<",">","\"", "'", "-"),
227 $str);
228 }
229
230 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
232}
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234// ------------------------------------------------------------------------
235
236/**
237 * Word Censoring Function
238 *
239 * Supply a string and an array of disallowed words and any
240 * matched words will be converted to #### or to the replacement
241 * word you've submitted.
242 *
243 * @access public
244 * @param string the text string
245 * @param string the array of censoered words
246 * @param string the optional replacement value
247 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200248 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000249if ( ! function_exists('word_censor'))
250{
251 function word_censor($str, $censored, $replacement = '')
252 {
253 if ( ! is_array($censored))
254 {
255 return $str;
256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
258 $str = ' '.$str.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000259
Derek Jonesf1b721a2009-01-21 17:52:13 +0000260 // \w, \b and a few others do not match on a unicode character
261 // set for performance reasons. As a result words like über
262 // will not match on a word boundary. Instead, we'll assume that
Derek Jonesdaf9c012010-03-05 10:29:30 -0600263 // a bad word will be bookeneded by any of these characters.
Derek Jonesf1b721a2009-01-21 17:52:13 +0000264 $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 foreach ($censored as $badword)
267 {
268 if ($replacement != '')
269 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000270 $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/i", "\\1{$replacement}\\3", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
272 else
273 {
Derek Jonesf1b721a2009-01-21 17:52:13 +0000274 $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 +0000275 }
276 }
Derek Jonesf1b721a2009-01-21 17:52:13 +0000277
Barry Mienydd671972010-10-04 16:33:58 +0200278 return trim($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
280}
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282// ------------------------------------------------------------------------
283
284/**
285 * Code Highlighter
286 *
287 * Colorizes code strings
288 *
289 * @access public
290 * @param string the text string
291 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200292 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000293if ( ! function_exists('highlight_code'))
294{
295 function highlight_code($str)
Barry Mienydd671972010-10-04 16:33:58 +0200296 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 // The highlight string function encodes and highlights
298 // brackets so we need them to start raw
299 $str = str_replace(array('&lt;', '&gt;'), array('<', '>'), $str);
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // Replace any existing PHP tags to temporary markers so they don't accidentally
302 // break the string out of PHP, and thus, thwart the highlighting.
Barry Mienydd671972010-10-04 16:33:58 +0200303 $str = str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'),
Andrey Andreev8edf87d2012-03-01 20:20:03 +0200304 array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
305 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000306
307 // The highlight_string function requires that the text be surrounded
308 // by PHP tags, which we will remove later
309 $str = '<?php '.$str.' ?>'; // <?
310
Barry Mienydd671972010-10-04 16:33:58 +0200311 // All the magic happens here, baby!
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 $str = highlight_string($str, TRUE);
313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // Remove our artificially added PHP, and the syntax highlighting that came with it
315 $str = preg_replace('/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i', '<span style="color: #$1">', $str);
316 $str = preg_replace('/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is', "$1</span>\n</span>\n</code>", $str);
317 $str = preg_replace('/<span style="color: #[A-Z0-9]+"\><\/span>/i', '', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // Replace our markers back to PHP tags.
Andrey Andreev8edf87d2012-03-01 20:20:03 +0200320 return str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
321 array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'),
322 $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
324}
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326// ------------------------------------------------------------------------
327
328/**
329 * Phrase Highlighter
330 *
331 * Highlights a phrase within a text string
332 *
333 * @access public
334 * @param string the text string
335 * @param string the phrase you'd like to highlight
336 * @param string the openging tag to precede the phrase with
337 * @param string the closing tag to end the phrase with
338 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200339 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000340if ( ! function_exists('highlight_phrase'))
341{
342 function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
343 {
344 if ($str == '')
345 {
346 return '';
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 if ($phrase != '')
350 {
351 return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);
352 }
353
354 return $str;
355 }
356}
Derek Jonesdaf9c012010-03-05 10:29:30 -0600357
358// ------------------------------------------------------------------------
359
360/**
361 * Convert Accented Foreign Characters to ASCII
362 *
363 * @access public
364 * @param string the text string
365 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200366 */
Derek Jonesdaf9c012010-03-05 10:29:30 -0600367if ( ! function_exists('convert_accented_characters'))
368{
Eric Barnes5e044802011-01-11 16:10:26 -0500369 function convert_accented_characters($str)
Derek Jonesdaf9c012010-03-05 10:29:30 -0600370 {
Greg Akerd96f8822011-12-27 16:23:47 -0600371 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
372 {
373 include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
374 }
375 elseif (is_file(APPPATH.'config/foreign_chars.php'))
376 {
377 include(APPPATH.'config/foreign_chars.php');
378 }
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Jonesdaf9c012010-03-05 10:29:30 -0600380 if ( ! isset($foreign_characters))
381 {
Eric Barnes5e044802011-01-11 16:10:26 -0500382 return $str;
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
391/**
392 * Word Wrap
393 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500394 * Wraps text at the specified character. Maintains the integrity of words.
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
396 * will URLs.
397 *
398 * @access public
399 * @param string the text string
400 * @param integer the number of characters to wrap at
401 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200402 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000403if ( ! function_exists('word_wrap'))
404{
405 function word_wrap($str, $charlim = '76')
406 {
407 // Se the character limit
408 if ( ! is_numeric($charlim))
409 $charlim = 76;
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // Reduce multiple spaces
412 $str = preg_replace("| +|", " ", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 // Standardize newlines
415 if (strpos($str, "\r") !== FALSE)
416 {
Barry Mienydd671972010-10-04 16:33:58 +0200417 $str = str_replace(array("\r\n", "\r"), "\n", $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
420 // If the current word is surrounded by {unwrap} tags we'll
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // strip the entire chunk and replace it with a marker.
422 $unwrap = array();
423 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
424 {
425 for ($i = 0; $i < count($matches['0']); $i++)
426 {
Barry Mienydd671972010-10-04 16:33:58 +0200427 $unwrap[] = $matches['1'][$i];
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
429 }
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
432 // Use PHP's native function to do the initial wordwrap.
433 // We set the cut flag to FALSE so that any individual words that are
Derek Jones37f4b9c2011-07-01 17:56:50 -0500434 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 $str = wordwrap($str, $charlim, "\n", FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 // Split the string into individual lines of text and cycle through them
438 $output = "";
Barry Mienydd671972010-10-04 16:33:58 +0200439 foreach (explode("\n", $str) as $line)
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
441 // Is the line within the allowed character count?
442 // If so we'll join it to the output and continue
443 if (strlen($line) <= $charlim)
444 {
Barry Mienydd671972010-10-04 16:33:58 +0200445 $output .= $line."\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 continue;
447 }
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 $temp = '';
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500450 while ((strlen($line)) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
452 // If the over-length word is a URL we won't wrap it
453 if (preg_match("!\[url.+\]|://|wwww.!", $line))
454 {
455 break;
456 }
457
458 // Trim the word down
459 $temp .= substr($line, 0, $charlim-1);
460 $line = substr($line, $charlim-1);
461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
463 // If $temp contains data it means we had to split up an over-length
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 // word into smaller chunks so we'll add it back to our current line
465 if ($temp != '')
466 {
Barry Mienydd671972010-10-04 16:33:58 +0200467 $output .= $temp."\n".$line;
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
469 else
470 {
471 $output .= $line;
472 }
473
474 $output .= "\n";
475 }
476
477 // Put our markers back
478 if (count($unwrap) > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200479 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 foreach ($unwrap as $key => $val)
481 {
482 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
483 }
484 }
485
486 // Remove the unwrap tags
487 $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
488
Barry Mienydd671972010-10-04 16:33:58 +0200489 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
491}
Barry Mienydd671972010-10-04 16:33:58 +0200492
Greg Akercbe32472010-08-05 14:09:20 -0500493// ------------------------------------------------------------------------
494
Greg Aker0f6b7c12010-08-05 14:11:14 -0500495/**
496 * Ellipsize String
497 *
498 * This function will strip tags from a string, split it at its max_length and ellipsize
499 *
Barry Mienydd671972010-10-04 16:33:58 +0200500 * @param string string to ellipsize
Greg Aker0f6b7c12010-08-05 14:11:14 -0500501 * @param integer max length of string
502 * @param mixed int (1|0) or float, .5, .2, etc for position to split
Barry Mienydd671972010-10-04 16:33:58 +0200503 * @param string ellipsis ; Default '...'
Greg Aker0f6b7c12010-08-05 14:11:14 -0500504 * @return string ellipsized string
505 */
Greg Akercbe32472010-08-05 14:09:20 -0500506if ( ! function_exists('ellipsize'))
507{
Greg Akercbe32472010-08-05 14:09:20 -0500508 function ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
509 {
510 // Strip tags
511 $str = trim(strip_tags($str));
512
513 // Is the string long enough to ellipsize?
514 if (strlen($str) <= $max_length)
515 {
516 return $str;
517 }
518
519 $beg = substr($str, 0, floor($max_length * $position));
520
521 $position = ($position > 1) ? 1 : $position;
522
523 if ($position === 1)
524 {
525 $end = substr($str, 0, -($max_length - strlen($beg)));
526 }
527 else
528 {
529 $end = substr($str, -($max_length - strlen($beg)));
530 }
531
Barry Mienydd671972010-10-04 16:33:58 +0200532 return $beg.$ellipsis.$end;
Greg Akercbe32472010-08-05 14:09:20 -0500533 }
534}
Derek Allard2067d1a2008-11-13 22:59:24 +0000535
536/* End of file text_helper.php */
Andrey Andreeve684bda2012-03-26 13:47:29 +0300537/* Location: ./system/helpers/text_helper.php */