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