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