Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
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 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame^] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame] | 8 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014, British Columbia Institute of Technology |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame^] | 11 | * 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 Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame^] | 18 | * 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 |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/) |
| 33 | * @license http://opensource.org/licenses/MIT MIT License |
| 34 | * @link http://codeigniter.com |
| 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * CodeIgniter Text Helpers |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Helpers |
| 45 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | * @link http://codeigniter.com/user_guide/helpers/text_helper.html |
| 48 | */ |
| 49 | |
| 50 | // ------------------------------------------------------------------------ |
| 51 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | if ( ! function_exists('word_limiter')) |
| 53 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 54 | /** |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | function word_limiter($str, $limit = 100, $end_char = '…') |
| 65 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 66 | if (trim($str) === '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | { |
| 68 | return $str; |
| 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 | preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 72 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 73 | if (strlen($str) === strlen($matches[0])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 74 | { |
| 75 | $end_char = ''; |
| 76 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 77 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | return rtrim($matches[0]).$end_char; |
| 79 | } |
| 80 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 81 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | // ------------------------------------------------------------------------ |
| 83 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | if ( ! function_exists('character_limiter')) |
| 85 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 86 | /** |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | function character_limiter($str, $n = 500, $end_char = '…') |
| 98 | { |
Mohammad Javad Naderi | c6f5ed1 | 2013-08-22 18:33:50 +0430 | [diff] [blame] | 99 | if (mb_strlen($str) < $n) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 100 | { |
| 101 | return $str; |
| 102 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | |
vlakoff | 62a5ee3 | 2012-09-04 23:12:49 +0200 | [diff] [blame] | 104 | // 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | |
Mohammad Javad Naderi | c6f5ed1 | 2013-08-22 18:33:50 +0430 | [diff] [blame] | 107 | if (mb_strlen($str) <= $n) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | { |
| 109 | return $str; |
| 110 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 111 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 112 | $out = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | foreach (explode(' ', trim($str)) as $val) |
| 114 | { |
Derek Jones | 01d6b4f | 2009-02-03 14:51:00 +0000 | [diff] [blame] | 115 | $out .= $val.' '; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 116 | |
Mohammad Javad Naderi | c6f5ed1 | 2013-08-22 18:33:50 +0430 | [diff] [blame] | 117 | if (mb_strlen($out) >= $n) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | { |
Derek Jones | 01d6b4f | 2009-02-03 14:51:00 +0000 | [diff] [blame] | 119 | $out = trim($out); |
Mohammad Javad Naderi | c6f5ed1 | 2013-08-22 18:33:50 +0430 | [diff] [blame] | 120 | return (mb_strlen($out) === mb_strlen($str)) ? $out : $out.$end_char; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 121 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | } |
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 | // ------------------------------------------------------------------------ |
| 127 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | if ( ! function_exists('ascii_to_entities')) |
| 129 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 130 | /** |
| 131 | * High ASCII to Entities |
| 132 | * |
Andrey Andreev | 7d75346 | 2012-10-27 03:37:40 +0300 | [diff] [blame] | 133 | * Converts high ASCII text and MS Word special characters to character entities |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 134 | * |
Andrey Andreev | 7d75346 | 2012-10-27 03:37:40 +0300 | [diff] [blame] | 135 | * @param string $str |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 136 | * @return string |
| 137 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | function ascii_to_entities($str) |
| 139 | { |
Andrey Andreev | 7d75346 | 2012-10-27 03:37:40 +0300 | [diff] [blame] | 140 | $out = ''; |
Andrey Andreev | 40235e6 | 2014-01-09 14:20:57 +0200 | [diff] [blame] | 141 | for ($i = 0, $s = strlen($str) - 1, $count = 1, $temp = array(); $i <= $s; $i++) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 142 | { |
| 143 | $ordinal = ord($str[$i]); |
| 144 | |
| 145 | if ($ordinal < 128) |
| 146 | { |
Derek Jones | 1978e12 | 2009-02-03 14:54:43 +0000 | [diff] [blame] | 147 | /* |
| 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 Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 151 | if (count($temp) === 1) |
Derek Jones | 1978e12 | 2009-02-03 14:54:43 +0000 | [diff] [blame] | 152 | { |
Andrey Andreev | 838a9d6 | 2012-12-03 14:37:47 +0200 | [diff] [blame] | 153 | $out .= '&#'.array_shift($temp).';'; |
Derek Jones | 1978e12 | 2009-02-03 14:54:43 +0000 | [diff] [blame] | 154 | $count = 1; |
| 155 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | |
Derek Jones | 1978e12 | 2009-02-03 14:54:43 +0000 | [diff] [blame] | 157 | $out .= $str[$i]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | } |
| 159 | else |
| 160 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 161 | if (count($temp) === 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | { |
| 163 | $count = ($ordinal < 224) ? 2 : 3; |
| 164 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 166 | $temp[] = $ordinal; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 168 | if (count($temp) === $count) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 169 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 170 | $number = ($count === 3) |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 171 | ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64) |
| 172 | : (($temp[0] % 32) * 64) + ($temp[1] % 64); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 173 | |
| 174 | $out .= '&#'.$number.';'; |
| 175 | $count = 1; |
| 176 | $temp = array(); |
| 177 | } |
Andrey Andreev | 40235e6 | 2014-01-09 14:20:57 +0200 | [diff] [blame] | 178 | // If this is the last iteration, just output whatever we have |
| 179 | elseif ($i === $s) |
| 180 | { |
| 181 | $out .= '&#'.implode(';', $temp).';'; |
| 182 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | return $out; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
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 | // ------------------------------------------------------------------------ |
| 191 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | if ( ! function_exists('entities_to_ascii')) |
| 193 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 194 | /** |
| 195 | * Entities to ASCII |
| 196 | * |
| 197 | * Converts character entities back to ASCII |
| 198 | * |
| 199 | * @param string |
| 200 | * @param bool |
| 201 | * @return string |
| 202 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | function entities_to_ascii($str, $all = TRUE) |
| 204 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) |
| 206 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 207 | for ($i = 0, $s = count($matches[0]); $i < $s; $i++) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 208 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 209 | $digits = $matches[1][$i]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 210 | $out = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 212 | if ($digits < 128) |
| 213 | { |
| 214 | $out .= chr($digits); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 216 | } |
| 217 | elseif ($digits < 2048) |
| 218 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 219 | $out .= chr(192 + (($digits - ($digits % 64)) / 64)).chr(128 + ($digits % 64)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 220 | } |
| 221 | else |
| 222 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 223 | $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)) |
| 224 | .chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)) |
| 225 | .chr(128 + ($digits % 64)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 226 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 228 | $str = str_replace($matches[0][$i], $out, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 229 | } |
| 230 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 232 | if ($all) |
| 233 | { |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 234 | return str_replace( |
| 235 | array('&', '<', '>', '"', ''', '-'), |
| 236 | array('&', '<', '>', '"', "'", '-'), |
| 237 | $str |
| 238 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 244 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 245 | // ------------------------------------------------------------------------ |
| 246 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | if ( ! function_exists('word_censor')) |
| 248 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 249 | /** |
| 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 |
| 257 | * @param string the array of censoered words |
| 258 | * @param string the optional replacement value |
| 259 | * @return string |
| 260 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | function word_censor($str, $censored, $replacement = '') |
| 262 | { |
| 263 | if ( ! is_array($censored)) |
| 264 | { |
| 265 | return $str; |
| 266 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 267 | |
| 268 | $str = ' '.$str.' '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 270 | // \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 Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 273 | // a bad word will be bookeneded by any of these characters. |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 274 | $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]'; |
| 275 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | foreach ($censored as $badword) |
| 277 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 278 | if ($replacement !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 279 | { |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 280 | $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] | 281 | } |
| 282 | else |
| 283 | { |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 284 | $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] | 285 | } |
| 286 | } |
Derek Jones | f1b721a | 2009-01-21 17:52:13 +0000 | [diff] [blame] | 287 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 288 | return trim($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 291 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | // ------------------------------------------------------------------------ |
| 293 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | if ( ! function_exists('highlight_code')) |
| 295 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 296 | /** |
| 297 | * Code Highlighter |
| 298 | * |
| 299 | * Colorizes code strings |
| 300 | * |
| 301 | * @param string the text string |
| 302 | * @return string |
| 303 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | function highlight_code($str) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 305 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 306 | /* 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 Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 313 | $str = str_replace( |
| 314 | array('<', '>', '<?', '?>', '<%', '%>', '\\', '</script>'), |
| 315 | array('<', '>', 'phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), |
| 316 | $str |
| 317 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | |
| 319 | // The highlight_string function requires that the text be surrounded |
| 320 | // by PHP tags, which we will remove later |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 321 | $str = highlight_string('<?php '.$str.' ?>', TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | // Remove our artificially added PHP, and the syntax highlighting that came with it |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 324 | $str = preg_replace( |
| 325 | array( |
| 326 | '/<span style="color: #([A-Z0-9]+)"><\?php( | )/i', |
| 327 | '/(<span style="color: #[A-Z0-9]+">.*?)\?><\/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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 337 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | // Replace our markers back to PHP tags. |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 339 | return str_replace( |
| 340 | array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), |
| 341 | array('<?', '?>', '<%', '%>', '\\', '</script>'), |
| 342 | $str |
| 343 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 346 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 347 | // ------------------------------------------------------------------------ |
| 348 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | if ( ! function_exists('highlight_phrase')) |
| 350 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 351 | /** |
| 352 | * Phrase Highlighter |
| 353 | * |
| 354 | * Highlights a phrase within a text string |
| 355 | * |
Andrey Andreev | ac023e1 | 2014-01-07 16:13:03 +0200 | [diff] [blame] | 356 | * @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 Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 360 | * @return string |
| 361 | */ |
Andrey Andreev | ac023e1 | 2014-01-07 16:13:03 +0200 | [diff] [blame] | 362 | function highlight_phrase($str, $phrase, $tag_open = '<mark>', $tag_close = '</mark>') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | { |
Andrey Andreev | ac023e1 | 2014-01-07 16:13:03 +0200 | [diff] [blame] | 364 | return ($str !== '' && $phrase !== '') |
Ivan Tcholakov | 8bc73eb | 2014-03-17 05:18:45 +0200 | [diff] [blame] | 365 | ? preg_replace('/('.preg_quote($phrase, '/').')/i'.(UTF8_ENABLED ? 'u' : ''), $tag_open.'\\1'.$tag_close, $str) |
Andrey Andreev | ac023e1 | 2014-01-07 16:13:03 +0200 | [diff] [blame] | 366 | : $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 369 | |
| 370 | // ------------------------------------------------------------------------ |
| 371 | |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 372 | if ( ! function_exists('convert_accented_characters')) |
| 373 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 374 | /** |
| 375 | * Convert Accented Foreign Characters to ASCII |
| 376 | * |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 377 | * @param string $str Input string |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 378 | * @return string |
| 379 | */ |
Eric Barnes | 5e04480 | 2011-01-11 16:10:26 -0500 | [diff] [blame] | 380 | function convert_accented_characters($str) |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 381 | { |
vlakoff | f5b4f6a | 2013-02-28 22:17:51 +0100 | [diff] [blame] | 382 | static $array_from, $array_to; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 383 | |
vlakoff | f5b4f6a | 2013-02-28 22:17:51 +0100 | [diff] [blame] | 384 | if ( ! is_array($array_from)) |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 385 | { |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 386 | if (file_exists(APPPATH.'config/foreign_chars.php')) |
Andrey Andreev | 0f2ec5b | 2012-01-16 14:02:24 +0200 | [diff] [blame] | 387 | { |
| 388 | include(APPPATH.'config/foreign_chars.php'); |
| 389 | } |
| 390 | |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 391 | if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php')) |
Andrey Andreev | 0f2ec5b | 2012-01-16 14:02:24 +0200 | [diff] [blame] | 392 | { |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 393 | include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'); |
| 394 | } |
| 395 | |
| 396 | if (empty($foreign_characters) OR ! is_array($foreign_characters)) |
| 397 | { |
vlakoff | f5b4f6a | 2013-02-28 22:17:51 +0100 | [diff] [blame] | 398 | $array_from = array(); |
| 399 | $array_to = array(); |
| 400 | |
Andrey Andreev | 0f2ec5b | 2012-01-16 14:02:24 +0200 | [diff] [blame] | 401 | return $str; |
| 402 | } |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 403 | |
vlakoff | f5b4f6a | 2013-02-28 22:17:51 +0100 | [diff] [blame] | 404 | $array_from = array_keys($foreign_characters); |
| 405 | $array_to = array_values($foreign_characters); |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 406 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 407 | |
vlakoff | f5b4f6a | 2013-02-28 22:17:51 +0100 | [diff] [blame] | 408 | return preg_replace($array_from, $array_to, $str); |
Derek Jones | daf9c01 | 2010-03-05 10:29:30 -0600 | [diff] [blame] | 409 | } |
| 410 | } |
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 | // ------------------------------------------------------------------------ |
| 413 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | if ( ! function_exists('word_wrap')) |
| 415 | { |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 416 | /** |
| 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 Andreev | fc44355 | 2012-01-07 02:14:55 +0200 | [diff] [blame] | 427 | function word_wrap($str, $charlim = 76) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 429 | // Set the character limit |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 430 | is_numeric($charlim) OR $charlim = 76; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 431 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | // Reduce multiple spaces |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 433 | $str = preg_replace('| +|', ' ', $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 434 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | // Standardize newlines |
| 436 | if (strpos($str, "\r") !== FALSE) |
| 437 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 438 | $str = str_replace(array("\r\n", "\r"), "\n", $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 439 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 440 | |
| 441 | // If the current word is surrounded by {unwrap} tags we'll |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 442 | // strip the entire chunk and replace it with a marker. |
| 443 | $unwrap = array(); |
vlakoff | 0f7eba2 | 2014-05-20 10:32:44 +0200 | [diff] [blame] | 444 | if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 446 | for ($i = 0, $c = count($matches[0]); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 447 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 448 | $unwrap[] = $matches[1][$i]; |
vlakoff | 0f7eba2 | 2014-05-20 10:32:44 +0200 | [diff] [blame] | 449 | $str = str_replace($matches[0][$i], '{{unwrapped'.$i.'}}', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 452 | |
| 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 Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 455 | // 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] | 456 | $str = wordwrap($str, $charlim, "\n", FALSE); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 457 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | // Split the string into individual lines of text and cycle through them |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 459 | $output = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 460 | foreach (explode("\n", $str) as $line) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | { |
| 462 | // Is the line within the allowed character count? |
| 463 | // If so we'll join it to the output and continue |
Andrey Andreev | 6ce4746 | 2014-02-13 03:28:00 +0200 | [diff] [blame] | 464 | if (mb_strlen($line) <= $charlim) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 466 | $output .= $line."\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | continue; |
| 468 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 469 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | $temp = ''; |
Andrey Andreev | 6ce4746 | 2014-02-13 03:28:00 +0200 | [diff] [blame] | 471 | while (mb_strlen($line) > $charlim) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | { |
| 473 | // If the over-length word is a URL we won't wrap it |
vlakoff | 2a8560c | 2014-05-20 10:32:35 +0200 | [diff] [blame] | 474 | if (preg_match('!\[url.+\]|://|www\.!', $line)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 475 | { |
| 476 | break; |
| 477 | } |
| 478 | |
| 479 | // Trim the word down |
Andrey Andreev | 6ce4746 | 2014-02-13 03:28:00 +0200 | [diff] [blame] | 480 | $temp .= mb_substr($line, 0, $charlim - 1); |
| 481 | $line = mb_substr($line, $charlim - 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 483 | |
| 484 | // 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] | 485 | // word into smaller chunks so we'll add it back to our current line |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 486 | if ($temp !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 487 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 488 | $output .= $temp."\n".$line."\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 489 | } |
| 490 | else |
| 491 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 492 | $output .= $line."\n"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 493 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // Put our markers back |
| 497 | if (count($unwrap) > 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 498 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 499 | foreach ($unwrap as $key => $val) |
| 500 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 501 | $output = str_replace('{{unwrapped'.$key.'}}', $val, $output); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
vlakoff | 0f7eba2 | 2014-05-20 10:32:44 +0200 | [diff] [blame] | 505 | return $output; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 508 | |
Greg Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 509 | // ------------------------------------------------------------------------ |
| 510 | |
| 511 | if ( ! function_exists('ellipsize')) |
| 512 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 513 | /** |
| 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 Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 524 | function ellipsize($str, $max_length, $position = 1, $ellipsis = '…') |
| 525 | { |
| 526 | // Strip tags |
| 527 | $str = trim(strip_tags($str)); |
| 528 | |
| 529 | // Is the string long enough to ellipsize? |
Andrey Andreev | 6ce4746 | 2014-02-13 03:28:00 +0200 | [diff] [blame] | 530 | if (mb_strlen($str) <= $max_length) |
Greg Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 531 | { |
| 532 | return $str; |
| 533 | } |
| 534 | |
Andrey Andreev | 6ce4746 | 2014-02-13 03:28:00 +0200 | [diff] [blame] | 535 | $beg = mb_substr($str, 0, floor($max_length * $position)); |
Greg Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 536 | $position = ($position > 1) ? 1 : $position; |
| 537 | |
| 538 | if ($position === 1) |
| 539 | { |
Andrey Andreev | 6ce4746 | 2014-02-13 03:28:00 +0200 | [diff] [blame] | 540 | $end = mb_substr($str, 0, -($max_length - mb_strlen($beg))); |
Greg Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 541 | } |
| 542 | else |
| 543 | { |
Andrey Andreev | 6ce4746 | 2014-02-13 03:28:00 +0200 | [diff] [blame] | 544 | $end = mb_substr($str, -($max_length - mb_strlen($beg))); |
Greg Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 545 | } |
| 546 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 547 | return $beg.$ellipsis.$end; |
Greg Aker | cbe3247 | 2010-08-05 14:09:20 -0500 | [diff] [blame] | 548 | } |
| 549 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 550 | |
| 551 | /* End of file text_helper.php */ |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame] | 552 | /* Location: ./system/helpers/text_helper.php */ |