Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [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 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 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 | 4921fed | 2012-01-07 01:28:07 +0200 | [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 | 4921fed | 2012-01-07 01:28:07 +0200 | [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 |
Andrey Andreev | c5a1f93 | 2012-01-24 15:29:02 +0200 | [diff] [blame^] | 12 | * bundled with this package in the files license.txt / license.rst. It is |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * CodeIgniter Text Helpers |
| 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Helpers |
| 33 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/helpers/text_helper.html |
| 36 | */ |
| 37 | |
| 38 | // ------------------------------------------------------------------------ |
| 39 | |
| 40 | /** |
| 41 | * Word Limiter |
| 42 | * |
| 43 | * Limits a string to X number of words. |
| 44 | * |
| 45 | * @access public |
| 46 | * @param string |
| 47 | * @param integer |
| 48 | * @param string the end character. Usually an ellipsis |
| 49 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 50 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | if ( ! function_exists('word_limiter')) |
| 52 | { |
| 53 | function word_limiter($str, $limit = 100, $end_char = '…') |
| 54 | { |
| 55 | if (trim($str) == '') |
| 56 | { |
| 57 | return $str; |
| 58 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 59 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 61 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 62 | if (strlen($str) === strlen($matches[0])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 63 | { |
| 64 | $end_char = ''; |
| 65 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 66 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | return rtrim($matches[0]).$end_char; |
| 68 | } |
| 69 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 70 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | // ------------------------------------------------------------------------ |
| 72 | |
| 73 | /** |
| 74 | * Character Limiter |
| 75 | * |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 76 | * Limits the string based on the character count. Preserves complete words |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | * so the character count may not be exactly as specified. |
| 78 | * |
| 79 | * @access public |
| 80 | * @param string |
| 81 | * @param integer |
| 82 | * @param string the end character. Usually an ellipsis |
| 83 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 84 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 85 | if ( ! function_exists('character_limiter')) |
| 86 | { |
| 87 | function character_limiter($str, $n = 500, $end_char = '…') |
| 88 | { |
| 89 | if (strlen($str) < $n) |
| 90 | { |
| 91 | return $str; |
| 92 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 93 | |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 94 | $str = preg_replace('/\s+/', ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | |
| 96 | if (strlen($str) <= $n) |
| 97 | { |
| 98 | return $str; |
| 99 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 100 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 101 | $out = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | foreach (explode(' ', trim($str)) as $val) |
| 103 | { |
Derek Jones | 01d6b4f | 2009-02-03 14:51:00 +0000 | [diff] [blame] | 104 | $out .= $val.' '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 105 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | if (strlen($out) >= $n) |
| 107 | { |
Derek Jones | 01d6b4f | 2009-02-03 14:51:00 +0000 | [diff] [blame] | 108 | $out = trim($out); |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 109 | return (strlen($out) === strlen($str)) ? $out : $out.$end_char; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 110 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 114 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | // ------------------------------------------------------------------------ |
| 116 | |
| 117 | /** |
| 118 | * High ASCII to Entities |
| 119 | * |
| 120 | * Converts High ascii text and MS Word special characters to character entities |
| 121 | * |
| 122 | * @access public |
| 123 | * @param string |
| 124 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | if ( ! function_exists('ascii_to_entities')) |
| 127 | { |
| 128 | function ascii_to_entities($str) |
| 129 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 130 | $count = 1; |
| 131 | $out = ''; |
| 132 | $temp = array(); |
| 133 | |
| 134 | for ($i = 0, $s = strlen($str); $i < $s; $i++) |
| 135 | { |
| 136 | $ordinal = ord($str[$i]); |
| 137 | |
| 138 | if ($ordinal < 128) |
| 139 | { |
Derek Jones | 1978e12 | 2009-02-03 14:54:43 +0000 | [diff] [blame] | 140 | /* |
| 141 | If the $temp array has a value but we have moved on, then it seems only |
| 142 | fair that we output that entity and restart $temp before continuing. -Paul |
| 143 | */ |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 144 | if (count($temp) === 1) |
Derek Jones | 1978e12 | 2009-02-03 14:54:43 +0000 | [diff] [blame] | 145 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 146 | $out .= '&#'.array_shift($temp).';'; |
Derek Jones | 1978e12 | 2009-02-03 14:54:43 +0000 | [diff] [blame] | 147 | $count = 1; |
| 148 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 149 | |
Derek Jones | 1978e12 | 2009-02-03 14:54:43 +0000 | [diff] [blame] | 150 | $out .= $str[$i]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | } |
| 152 | else |
| 153 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 154 | if (count($temp) === 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | { |
| 156 | $count = ($ordinal < 224) ? 2 : 3; |
| 157 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 159 | $temp[] = $ordinal; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 161 | if (count($temp) === $count) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 163 | $number = ($count === 3) |
| 164 | ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64) |
| 165 | : (($temp[0] % 32) * 64) + ($temp[1] % 64); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 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 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 196 | for ($i = 0, $s = count($matches[0]); $i < $s; $i++) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 197 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 198 | $digits = $matches[1][$i]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 199 | $out = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 201 | if ($digits < 128) |
| 202 | { |
| 203 | $out .= chr($digits); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | } |
| 206 | elseif ($digits < 2048) |
| 207 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 208 | $out .= chr(192 + (($digits - ($digits % 64)) / 64)).chr(128 + ($digits % 64)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 209 | } |
| 210 | else |
| 211 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 212 | $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)) |
| 213 | .chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)) |
| 214 | .chr(128 + ($digits % 64)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 215 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 217 | $str = str_replace($matches[0][$i], $out, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 218 | } |
| 219 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 221 | if ($all) |
| 222 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 223 | return str_replace(array('&', '<', '>', '"', ''', '-'), |
| 224 | array('&', '<', '>', '"', "'", '-'), |
| 225 | $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 231 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | // ------------------------------------------------------------------------ |
| 233 | |
| 234 | /** |
| 235 | * Word Censoring Function |
| 236 | * |
| 237 | * Supply a string and an array of disallowed words and any |
| 238 | * matched words will be converted to #### or to the replacement |
| 239 | * word you've submitted. |
| 240 | * |
| 241 | * @access public |
| 242 | * @param string the text string |
| 243 | * @param string the array of censoered words |
| 244 | * @param string the optional replacement value |
| 245 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 246 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | if ( ! function_exists('word_censor')) |
| 248 | { |
| 249 | function word_censor($str, $censored, $replacement = '') |
| 250 | { |
| 251 | if ( ! is_array($censored)) |
| 252 | { |
| 253 | return $str; |
| 254 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 255 | |
| 256 | $str = ' '.$str.' '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 258 | // \w, \b and a few others do not match on a unicode character |
| 259 | // set for performance reasons. As a result words like über |
| 260 | // will not match on a word boundary. Instead, we'll assume that |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 261 | // a bad word will be bookeneded by any of these characters. |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 262 | $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]'; |
| 263 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | foreach ($censored as $badword) |
| 265 | { |
| 266 | if ($replacement != '') |
| 267 | { |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 268 | $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] | 269 | } |
| 270 | else |
| 271 | { |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 272 | $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] | 273 | } |
| 274 | } |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 275 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 276 | return trim($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 279 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | // ------------------------------------------------------------------------ |
| 281 | |
| 282 | /** |
| 283 | * Code Highlighter |
| 284 | * |
| 285 | * Colorizes code strings |
| 286 | * |
| 287 | * @access public |
| 288 | * @param string the text string |
| 289 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 290 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | if ( ! function_exists('highlight_code')) |
| 292 | { |
| 293 | function highlight_code($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 294 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 295 | /* The highlight string function encodes and highlights |
| 296 | * brackets so we need them to start raw. |
| 297 | * |
| 298 | * Also replace any existing PHP tags to temporary markers |
| 299 | * so they don't accidentally break the string out of PHP, |
| 300 | * and thus, thwart the highlighting. |
| 301 | */ |
| 302 | $str = str_replace(array('<', '>', '<?', '?>', '<%', '%>', '\\', '</script>'), |
| 303 | array('<', '>', 'phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), |
| 304 | $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | |
| 306 | // The highlight_string function requires that the text be surrounded |
| 307 | // by PHP tags, which we will remove later |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 308 | $str = highlight_string('<?php '.$str.' ?>', TRUE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 309 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | // Remove our artificially added PHP, and the syntax highlighting that came with it |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 311 | $str = preg_replace(array( |
| 312 | '/<span style="color: #([A-Z0-9]+)"><\?php( | )/i', |
| 313 | '/(<span style="color: #[A-Z0-9]+">.*?)\?><\/span>\n<\/span>\n<\/code>/is', |
| 314 | '/<span style="color: #[A-Z0-9]+"\><\/span>/i' |
| 315 | ), |
| 316 | array( |
| 317 | '<span style="color: #$1">', |
| 318 | "$1</span>\n</span>\n</code>", |
| 319 | '' |
| 320 | ), |
| 321 | $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 322 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | // Replace our markers back to PHP tags. |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 324 | return str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), |
| 325 | array('<?', '?>', '<%', '%>', '\\', '</script>'), |
| 326 | $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 329 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | // ------------------------------------------------------------------------ |
| 331 | |
| 332 | /** |
| 333 | * Phrase Highlighter |
| 334 | * |
| 335 | * Highlights a phrase within a text string |
| 336 | * |
| 337 | * @access public |
| 338 | * @param string the text string |
| 339 | * @param string the phrase you'd like to highlight |
| 340 | * @param string the openging tag to precede the phrase with |
| 341 | * @param string the closing tag to end the phrase with |
| 342 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 343 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | if ( ! function_exists('highlight_phrase')) |
| 345 | { |
| 346 | function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>') |
| 347 | { |
| 348 | if ($str == '') |
| 349 | { |
| 350 | return ''; |
| 351 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 352 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | if ($phrase != '') |
| 354 | { |
Andrey Andreev | cb324bd | 2012-01-08 07:06:35 +0200 | [diff] [blame] | 355 | return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open.'\\1'.$tag_close, $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | return $str; |
| 359 | } |
| 360 | } |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 361 | |
| 362 | // ------------------------------------------------------------------------ |
| 363 | |
| 364 | /** |
| 365 | * Convert Accented Foreign Characters to ASCII |
| 366 | * |
| 367 | * @access public |
| 368 | * @param string the text string |
| 369 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 370 | */ |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 371 | if ( ! function_exists('convert_accented_characters')) |
| 372 | { |
Eric Barnes | 5e04480 | 2011-01-11 16:10:26 -0500 | [diff] [blame] | 373 | function convert_accented_characters($str) |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 374 | { |
Andrey Andreev | 0f2ec5b | 2012-01-16 14:02:24 +0200 | [diff] [blame] | 375 | global $foreign_characters; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 376 | |
Andrey Andreev | 0f2ec5b | 2012-01-16 14:02:24 +0200 | [diff] [blame] | 377 | if ( ! isset($foreign_characters) OR ! is_array($foreign_characters)) |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 378 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 379 | if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php')) |
Andrey Andreev | 0f2ec5b | 2012-01-16 14:02:24 +0200 | [diff] [blame] | 380 | { |
| 381 | include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'); |
| 382 | } |
| 383 | elseif (is_file(APPPATH.'config/foreign_chars.php')) |
| 384 | { |
| 385 | include(APPPATH.'config/foreign_chars.php'); |
| 386 | } |
| 387 | |
| 388 | if ( ! isset($foreign_characters) OR ! is_array($foreign_chars)) |
| 389 | { |
| 390 | return $str; |
| 391 | } |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 392 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 393 | |
Eric Barnes | 5e04480 | 2011-01-11 16:10:26 -0500 | [diff] [blame] | 394 | return preg_replace(array_keys($foreign_characters), array_values($foreign_characters), $str); |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 395 | } |
| 396 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 397 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | // ------------------------------------------------------------------------ |
| 399 | |
| 400 | /** |
| 401 | * Word Wrap |
| 402 | * |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 403 | * Wraps text at the specified character. Maintains the integrity of words. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor |
| 405 | * will URLs. |
| 406 | * |
| 407 | * @access public |
| 408 | * @param string the text string |
| 409 | * @param integer the number of characters to wrap at |
| 410 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 411 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | if ( ! function_exists('word_wrap')) |
| 413 | { |
Andrey Andreev | fc44355 | 2012-01-07 02:14:55 +0200 | [diff] [blame] | 414 | function word_wrap($str, $charlim = 76) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 416 | // Set the character limit |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | if ( ! is_numeric($charlim)) |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 418 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | $charlim = 76; |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 420 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 421 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | // Reduce multiple spaces |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 423 | $str = preg_replace('| +|', ' ', $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 424 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | // Standardize newlines |
| 426 | if (strpos($str, "\r") !== FALSE) |
| 427 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 428 | $str = str_replace(array("\r\n", "\r"), "\n", $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 430 | |
| 431 | // If the current word is surrounded by {unwrap} tags we'll |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | // strip the entire chunk and replace it with a marker. |
| 433 | $unwrap = array(); |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 434 | if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 436 | for ($i = 0, $c = count($matches[0]); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 438 | $unwrap[] = $matches[1][$i]; |
| 439 | $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 442 | |
| 443 | // Use PHP's native function to do the initial wordwrap. |
| 444 | // We set the cut flag to FALSE so that any individual words that are |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 445 | // 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] | 446 | $str = wordwrap($str, $charlim, "\n", FALSE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 447 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | // Split the string into individual lines of text and cycle through them |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 449 | $output = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 450 | foreach (explode("\n", $str) as $line) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | { |
| 452 | // Is the line within the allowed character count? |
| 453 | // If so we'll join it to the output and continue |
| 454 | if (strlen($line) <= $charlim) |
| 455 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 456 | $output .= $line."\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | continue; |
| 458 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 459 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | $temp = ''; |
Pascal Kriete | 45e3cdf | 2011-02-14 13:26:20 -0500 | [diff] [blame] | 461 | while ((strlen($line)) > $charlim) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | { |
| 463 | // If the over-length word is a URL we won't wrap it |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 464 | if (preg_match('!\[url.+\]|://|wwww.!', $line)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | { |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | // Trim the word down |
| 470 | $temp .= substr($line, 0, $charlim-1); |
| 471 | $line = substr($line, $charlim-1); |
| 472 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 473 | |
| 474 | // 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] | 475 | // word into smaller chunks so we'll add it back to our current line |
| 476 | if ($temp != '') |
| 477 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 478 | $output .= $temp."\n".$line."\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 479 | } |
| 480 | else |
| 481 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 482 | $output .= $line."\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 483 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | // Put our markers back |
| 487 | if (count($unwrap) > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 488 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 489 | foreach ($unwrap as $key => $val) |
| 490 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 491 | $output = str_replace('{{unwrapped'.$key.'}}', $val, $output); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 495 | // Remove the unwrap tags and return |
| 496 | return str_replace(array('{unwrap}', '{/unwrap}'), '', $output); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 499 | |
Greg Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 500 | // ------------------------------------------------------------------------ |
| 501 | |
Greg Aker | 0f6b7c1 | 2010-08-05 14:11:14 -0500 | [diff] [blame] | 502 | /** |
| 503 | * Ellipsize String |
| 504 | * |
| 505 | * This function will strip tags from a string, split it at its max_length and ellipsize |
| 506 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 507 | * @param string string to ellipsize |
Greg Aker | 0f6b7c1 | 2010-08-05 14:11:14 -0500 | [diff] [blame] | 508 | * @param integer max length of string |
| 509 | * @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] | 510 | * @param string ellipsis ; Default '...' |
Greg Aker | 0f6b7c1 | 2010-08-05 14:11:14 -0500 | [diff] [blame] | 511 | * @return string ellipsized string |
| 512 | */ |
Greg Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 513 | if ( ! function_exists('ellipsize')) |
| 514 | { |
Greg Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 515 | function ellipsize($str, $max_length, $position = 1, $ellipsis = '…') |
| 516 | { |
| 517 | // Strip tags |
| 518 | $str = trim(strip_tags($str)); |
| 519 | |
| 520 | // Is the string long enough to ellipsize? |
| 521 | if (strlen($str) <= $max_length) |
| 522 | { |
| 523 | return $str; |
| 524 | } |
| 525 | |
| 526 | $beg = substr($str, 0, floor($max_length * $position)); |
Greg Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 527 | $position = ($position > 1) ? 1 : $position; |
| 528 | |
| 529 | if ($position === 1) |
| 530 | { |
| 531 | $end = substr($str, 0, -($max_length - strlen($beg))); |
| 532 | } |
| 533 | else |
| 534 | { |
| 535 | $end = substr($str, -($max_length - strlen($beg))); |
| 536 | } |
| 537 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 538 | return $beg.$ellipsis.$end; |
Greg Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 539 | } |
| 540 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 541 | |
| 542 | /* End of file text_helper.php */ |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 543 | /* Location: ./system/helpers/text_helper.php */ |