Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame^] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame^] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame^] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @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 Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | * @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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 52 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 53 | if ( ! function_exists('word_limiter')) |
| 54 | { |
| 55 | function word_limiter($str, $limit = 100, $end_char = '…') |
| 56 | { |
| 57 | if (trim($str) == '') |
| 58 | { |
| 59 | return $str; |
| 60 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 61 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 63 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | if (strlen($str) == strlen($matches[0])) |
| 65 | { |
| 66 | $end_char = ''; |
| 67 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 68 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | return rtrim($matches[0]).$end_char; |
| 70 | } |
| 71 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 72 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | // ------------------------------------------------------------------------ |
| 74 | |
| 75 | /** |
| 76 | * Character Limiter |
| 77 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 78 | * Limits the string based on the character count. Preserves complete words |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | * 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 86 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | if ( ! function_exists('character_limiter')) |
| 88 | { |
| 89 | function character_limiter($str, $n = 500, $end_char = '…') |
| 90 | { |
| 91 | if (strlen($str) < $n) |
| 92 | { |
| 93 | return $str; |
| 94 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 95 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str)); |
| 97 | |
| 98 | if (strlen($str) <= $n) |
| 99 | { |
| 100 | return $str; |
| 101 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 102 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | $out = ""; |
| 104 | foreach (explode(' ', trim($str)) as $val) |
| 105 | { |
Derek Jones | 01d6b4f | 2009-02-03 14:51:00 +0000 | [diff] [blame] | 106 | $out .= $val.' '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 107 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | if (strlen($out) >= $n) |
| 109 | { |
Derek Jones | 01d6b4f | 2009-02-03 14:51:00 +0000 | [diff] [blame] | 110 | $out = trim($out); |
| 111 | return (strlen($out) == strlen($str)) ? $out : $out.$end_char; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 112 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 116 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | // ------------------------------------------------------------------------ |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 127 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | if ( ! function_exists('ascii_to_entities')) |
| 129 | { |
| 130 | function ascii_to_entities($str) |
| 131 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 132 | $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 Jones | 1978e12 | 2009-02-03 14:54:43 +0000 | [diff] [blame] | 142 | /* |
| 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 Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 148 | $out .= '&#'.array_shift($temp).';'; |
Derek Jones | 1978e12 | 2009-02-03 14:54:43 +0000 | [diff] [blame] | 149 | $count = 1; |
| 150 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | |
Derek Jones | 1978e12 | 2009-02-03 14:54:43 +0000 | [diff] [blame] | 152 | $out .= $str[$i]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 153 | } |
| 154 | else |
| 155 | { |
| 156 | if (count($temp) == 0) |
| 157 | { |
| 158 | $count = ($ordinal < 224) ? 2 : 3; |
| 159 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 161 | $temp[] = $ordinal; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 177 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | // ------------------------------------------------------------------------ |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | if ( ! function_exists('entities_to_ascii')) |
| 191 | { |
| 192 | function entities_to_ascii($str, $all = TRUE) |
| 193 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 194 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 200 | $out = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 202 | if ($digits < 128) |
| 203 | { |
| 204 | $out .= chr($digits); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 206 | } |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 219 | $str = str_replace($matches['0'][$i], $out, $str); |
| 220 | } |
| 221 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | if ($all) |
| 224 | { |
| 225 | $str = str_replace(array("&", "<", ">", """, "'", "-"), |
| 226 | array("&","<",">","\"", "'", "-"), |
| 227 | $str); |
| 228 | } |
| 229 | |
| 230 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 233 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | // ------------------------------------------------------------------------ |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 248 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | if ( ! function_exists('word_censor')) |
| 250 | { |
| 251 | function word_censor($str, $censored, $replacement = '') |
| 252 | { |
| 253 | if ( ! is_array($censored)) |
| 254 | { |
| 255 | return $str; |
| 256 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 257 | |
| 258 | $str = ' '.$str.' '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 260 | // \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 Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 263 | // a bad word will be bookeneded by any of these characters. |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 264 | $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]'; |
| 265 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | foreach ($censored as $badword) |
| 267 | { |
| 268 | if ($replacement != '') |
| 269 | { |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 270 | $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/i", "\\1{$replacement}\\3", $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | } |
| 272 | else |
| 273 | { |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 274 | $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/ie", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'", $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 277 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 278 | return trim($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 281 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 282 | // ------------------------------------------------------------------------ |
| 283 | |
| 284 | /** |
| 285 | * Code Highlighter |
| 286 | * |
| 287 | * Colorizes code strings |
| 288 | * |
| 289 | * @access public |
| 290 | * @param string the text string |
| 291 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 292 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | if ( ! function_exists('highlight_code')) |
| 294 | { |
| 295 | function highlight_code($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 296 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | // The highlight string function encodes and highlights |
| 298 | // brackets so we need them to start raw |
| 299 | $str = str_replace(array('<', '>'), array('<', '>'), $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 300 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | // 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 303 | $str = str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'), |
Andrey Andreev | 8edf87d | 2012-03-01 20:20:03 +0200 | [diff] [blame] | 304 | array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), |
| 305 | $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 306 | |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 311 | // All the magic happens here, baby! |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | $str = highlight_string($str, TRUE); |
| 313 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | // Remove our artificially added PHP, and the syntax highlighting that came with it |
| 315 | $str = preg_replace('/<span style="color: #([A-Z0-9]+)"><\?php( | )/i', '<span style="color: #$1">', $str); |
| 316 | $str = preg_replace('/(<span style="color: #[A-Z0-9]+">.*?)\?><\/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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 318 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | // Replace our markers back to PHP tags. |
Andrey Andreev | 8edf87d | 2012-03-01 20:20:03 +0200 | [diff] [blame] | 320 | return str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), |
| 321 | array('<?', '?>', '<%', '%>', '\\', '</script>'), |
| 322 | $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 325 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | // ------------------------------------------------------------------------ |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 339 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | if ( ! 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 348 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | 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 Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 357 | |
| 358 | // ------------------------------------------------------------------------ |
| 359 | |
| 360 | /** |
| 361 | * Convert Accented Foreign Characters to ASCII |
| 362 | * |
| 363 | * @access public |
| 364 | * @param string the text string |
| 365 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 366 | */ |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 367 | if ( ! function_exists('convert_accented_characters')) |
| 368 | { |
Eric Barnes | 5e04480 | 2011-01-11 16:10:26 -0500 | [diff] [blame] | 369 | function convert_accented_characters($str) |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 370 | { |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 371 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 379 | |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 380 | if ( ! isset($foreign_characters)) |
| 381 | { |
Eric Barnes | 5e04480 | 2011-01-11 16:10:26 -0500 | [diff] [blame] | 382 | return $str; |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 383 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 384 | |
Eric Barnes | 5e04480 | 2011-01-11 16:10:26 -0500 | [diff] [blame] | 385 | return preg_replace(array_keys($foreign_characters), array_values($foreign_characters), $str); |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 386 | } |
| 387 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 388 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | // ------------------------------------------------------------------------ |
| 390 | |
| 391 | /** |
| 392 | * Word Wrap |
| 393 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 394 | * Wraps text at the specified character. Maintains the integrity of words. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | * 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 402 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | if ( ! 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 410 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | // Reduce multiple spaces |
| 412 | $str = preg_replace("| +|", " ", $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 413 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | // Standardize newlines |
| 415 | if (strpos($str, "\r") !== FALSE) |
| 416 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 417 | $str = str_replace(array("\r\n", "\r"), "\n", $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 419 | |
| 420 | // If the current word is surrounded by {unwrap} tags we'll |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | // 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 427 | $unwrap[] = $matches['1'][$i]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str); |
| 429 | } |
| 430 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 431 | |
| 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 Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 434 | // too long get left alone. In the next step we'll deal with them. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | $str = wordwrap($str, $charlim, "\n", FALSE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 436 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | // Split the string into individual lines of text and cycle through them |
| 438 | $output = ""; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 439 | foreach (explode("\n", $str) as $line) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 440 | { |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 445 | $output .= $line."\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | continue; |
| 447 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 448 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | $temp = ''; |
Pascal Kriete | 45e3cdf | 2011-02-14 13:26:20 -0500 | [diff] [blame] | 450 | while ((strlen($line)) > $charlim) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | { |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 462 | |
| 463 | // If $temp contains data it means we had to split up an over-length |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | // word into smaller chunks so we'll add it back to our current line |
| 465 | if ($temp != '') |
| 466 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 467 | $output .= $temp."\n".$line; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 468 | } |
| 469 | else |
| 470 | { |
| 471 | $output .= $line; |
| 472 | } |
| 473 | |
| 474 | $output .= "\n"; |
| 475 | } |
| 476 | |
| 477 | // Put our markers back |
| 478 | if (count($unwrap) > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 479 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 489 | return $output; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 492 | |
Greg Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 493 | // ------------------------------------------------------------------------ |
| 494 | |
Greg Aker | 0f6b7c1 | 2010-08-05 14:11:14 -0500 | [diff] [blame] | 495 | /** |
| 496 | * Ellipsize String |
| 497 | * |
| 498 | * This function will strip tags from a string, split it at its max_length and ellipsize |
| 499 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 500 | * @param string string to ellipsize |
Greg Aker | 0f6b7c1 | 2010-08-05 14:11:14 -0500 | [diff] [blame] | 501 | * @param integer max length of string |
| 502 | * @param mixed int (1|0) or float, .5, .2, etc for position to split |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 503 | * @param string ellipsis ; Default '...' |
Greg Aker | 0f6b7c1 | 2010-08-05 14:11:14 -0500 | [diff] [blame] | 504 | * @return string ellipsized string |
| 505 | */ |
Greg Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 506 | if ( ! function_exists('ellipsize')) |
| 507 | { |
Greg Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 508 | function ellipsize($str, $max_length, $position = 1, $ellipsis = '…') |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 532 | return $beg.$ellipsis.$end; |
Greg Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 533 | } |
| 534 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | |
| 536 | /* End of file text_helper.php */ |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame^] | 537 | /* Location: ./system/helpers/text_helper.php */ |