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