admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Code Igniter Text Helpers |
| 20 | * |
| 21 | * @package CodeIgniter |
| 22 | * @subpackage Helpers |
| 23 | * @category Helpers |
| 24 | * @author Rick Ellis |
| 25 | * @link http://www.codeigniter.com/user_guide/helpers/text_helper.html |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * Word Limiter |
| 32 | * |
| 33 | * Limits a string to X number of words. |
| 34 | * |
| 35 | * @access public |
| 36 | * @param string |
| 37 | * @param integer |
| 38 | * @param string the end character. Usually an ellipsis |
| 39 | * @return string |
| 40 | */ |
| 41 | function word_limiter($str, $n = 100, $end_char = '…') |
| 42 | { |
| 43 | if (strlen($str) < $n) |
| 44 | { |
| 45 | return $str; |
| 46 | } |
| 47 | |
| 48 | $words = explode(' ', preg_replace("/\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", " ", $str))); |
| 49 | |
| 50 | if (count($words) <= $n) |
| 51 | { |
| 52 | return $str; |
| 53 | } |
| 54 | |
| 55 | $str = ''; |
| 56 | for ($i = 0; $i < $n; $i++) |
| 57 | { |
| 58 | $str .= $words[$i].' '; |
| 59 | } |
| 60 | |
| 61 | return trim($str).$end_char; |
| 62 | } |
| 63 | |
| 64 | // ------------------------------------------------------------------------ |
| 65 | |
| 66 | /** |
| 67 | * Character Limiter |
| 68 | * |
| 69 | * Limits the string based on the character count. Preserves complete words |
| 70 | * so the character count may not be exactly as specified. |
| 71 | * |
| 72 | * @access public |
| 73 | * @param string |
| 74 | * @param integer |
| 75 | * @param string the end character. Usually an ellipsis |
| 76 | * @return string |
| 77 | */ |
| 78 | function character_limiter($str, $n = 500, $end_char = '…') |
| 79 | { |
| 80 | if (strlen($str) < $n) |
| 81 | { |
| 82 | return $str; |
| 83 | } |
| 84 | |
| 85 | $str = preg_replace("/\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", " ", $str)); |
| 86 | |
| 87 | if (strlen($str) <= $n) |
| 88 | { |
| 89 | return $str; |
| 90 | } |
| 91 | |
| 92 | $out = ""; |
| 93 | foreach (explode(' ', trim($str)) as $val) |
| 94 | { |
| 95 | $out .= $val.' '; |
| 96 | if (strlen($out) >= $n) |
| 97 | { |
| 98 | return trim($out).$end_char; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // ------------------------------------------------------------------------ |
| 104 | |
| 105 | /** |
| 106 | * High ASCII to Entities |
| 107 | * |
| 108 | * Converts High ascii text and MS Word special characters to character entities |
| 109 | * |
| 110 | * @access public |
| 111 | * @param string |
| 112 | * @return string |
| 113 | */ |
| 114 | function ascii_to_entities($str) |
| 115 | { |
| 116 | $count = 1; |
| 117 | $out = ''; |
| 118 | $temp = array(); |
| 119 | |
| 120 | for ($i = 0, $s = strlen($str); $i < $s; $i++) |
| 121 | { |
| 122 | $ordinal = ord($str[$i]); |
| 123 | |
| 124 | if ($ordinal < 128) |
| 125 | { |
| 126 | $out .= $str[$i]; |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | if (count($temp) == 0) |
| 131 | { |
| 132 | $count = ($ordinal < 224) ? 2 : 3; |
| 133 | } |
| 134 | |
| 135 | $temp[] = $ordinal; |
| 136 | |
| 137 | if (count($temp) == $count) |
| 138 | { |
| 139 | $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); |
| 140 | |
| 141 | $out .= '&#'.$number.';'; |
| 142 | $count = 1; |
| 143 | $temp = array(); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return $out; |
| 149 | } |
| 150 | |
| 151 | // ------------------------------------------------------------------------ |
| 152 | |
| 153 | /** |
| 154 | * Entities to ASCII |
| 155 | * |
| 156 | * Converts character entities back to ASCII |
| 157 | * |
| 158 | * @access public |
| 159 | * @param string |
| 160 | * @param bool |
| 161 | * @return string |
| 162 | */ |
| 163 | function entities_to_ascii($str, $all = TRUE) |
| 164 | { |
| 165 | if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) |
| 166 | { |
| 167 | for ($i = 0, $s = count($matches['0']); $i < $s; $i++) |
| 168 | { |
| 169 | $digits = $matches['1'][$i]; |
| 170 | |
| 171 | $out = ''; |
| 172 | |
| 173 | if ($digits < 128) |
| 174 | { |
| 175 | $out .= chr($digits); |
| 176 | |
| 177 | } |
| 178 | elseif ($digits < 2048) |
| 179 | { |
| 180 | $out .= chr(192 + (($digits - ($digits % 64)) / 64)); |
| 181 | $out .= chr(128 + ($digits % 64)); |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | $out .= chr(224 + (($digits - ($digits % 4096)) / 4096)); |
| 186 | $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64)); |
| 187 | $out .= chr(128 + ($digits % 64)); |
| 188 | } |
| 189 | |
| 190 | $str = str_replace($matches['0'][$i], $out, $str); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if ($all) |
| 195 | { |
| 196 | $str = str_replace(array("&", "<", ">", """, "'", "-"), |
| 197 | array("&","<",">","\"", "'", "-"), |
| 198 | $str); |
| 199 | } |
| 200 | |
| 201 | return $str; |
| 202 | } |
| 203 | |
| 204 | // ------------------------------------------------------------------------ |
| 205 | |
| 206 | /** |
| 207 | * Word Censoring Function |
| 208 | * |
| 209 | * Supply a string and an array of disallowed words and any |
| 210 | * matched words will be converted to #### or to the replacement |
| 211 | * word you've submitted. |
| 212 | * |
| 213 | * @access public |
| 214 | * @param string the text string |
| 215 | * @param string the array of censoered words |
| 216 | * @param string the optional replacement value |
| 217 | * @return string |
| 218 | */ |
| 219 | function word_censor($str, $censored, $replacement = '') |
| 220 | { |
| 221 | if ( ! is_array($censored)) |
| 222 | { |
| 223 | return $str; |
| 224 | } |
| 225 | |
| 226 | $str = ' '.$str.' '; |
| 227 | foreach ($censored as $badword) |
| 228 | { |
| 229 | if ($replacement != '') |
| 230 | { |
| 231 | $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str); |
| 232 | } |
| 233 | else |
| 234 | { |
| 235 | $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | return trim($str); |
| 240 | } |
| 241 | |
| 242 | // ------------------------------------------------------------------------ |
| 243 | |
| 244 | /** |
| 245 | * Code Highlighter |
| 246 | * |
| 247 | * Colorizes code strings |
| 248 | * |
| 249 | * @access public |
| 250 | * @param string the text string |
| 251 | * @return string |
| 252 | */ |
| 253 | function highlight_code($str) |
| 254 | { |
| 255 | // The highlight string function encodes and highlights |
| 256 | // brackets so we need them to start raw |
| 257 | $str = str_replace(array('<', '>'), array('<', '>'), $str); |
| 258 | |
| 259 | // Replace any existing PHP tags to temporary markers so they don't accidentally |
| 260 | // break the string out of PHP, and thus, thwart the highlighting. |
| 261 | |
| 262 | $str = str_replace(array('<?php', '?>', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str); |
| 263 | |
| 264 | // The highlight_string function requires that the text be surrounded |
| 265 | // by PHP tags. Since we don't know if A) the submitted text has PHP tags, |
| 266 | // or B) whether the PHP tags enclose the entire string, we will add our |
| 267 | // own PHP tags around the string along with some markers to make replacement easier later |
| 268 | |
| 269 | $str = '<?php //tempstart'."\n".$str.'//tempend ?>'; // <? |
| 270 | |
| 271 | // All the magic happens here, baby! |
| 272 | $str = highlight_string($str, TRUE); |
| 273 | |
| 274 | // Prior to PHP 5, the highlight function used icky font tags |
| 275 | // so we'll replace them with span tags. |
| 276 | if (abs(phpversion()) < 5) |
| 277 | { |
| 278 | $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str); |
| 279 | $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); |
| 280 | } |
| 281 | |
| 282 | // Remove our artificially added PHP |
| 283 | $str = preg_replace("#\<code\>.+?//tempstart\<br />\</span\>#is", "<code>\n", $str); |
| 284 | $str = preg_replace("#\<code\>.+?//tempstart\<br />#is", "<code>\n", $str); |
| 285 | $str = preg_replace("#//tempend.+#is", "</span>\n</code>", $str); |
| 286 | |
| 287 | // Replace our markers back to PHP tags. |
| 288 | $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('<?php', '?>', '\\'), $str); //<? |
| 289 | |
| 290 | return $str; |
| 291 | } |
| 292 | |
| 293 | // ------------------------------------------------------------------------ |
| 294 | |
| 295 | /** |
| 296 | * Phrase Highlighter |
| 297 | * |
| 298 | * Highlights a phrase within a text string |
| 299 | * |
| 300 | * @access public |
| 301 | * @param string the text string |
| 302 | * @param string the phrase you'd like to highlight |
| 303 | * @param string the openging tag to precede the phrase with |
| 304 | * @param string the closing tag to end the phrase with |
| 305 | * @return string |
| 306 | */ |
| 307 | function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>') |
| 308 | { |
| 309 | if ($str == '') |
| 310 | { |
| 311 | return ''; |
| 312 | } |
| 313 | |
| 314 | if ($phrase != '') |
| 315 | { |
| 316 | return preg_replace('/('.preg_quote($phrase).')/i', $tag_open."\\1".$tag_close, $str); |
| 317 | } |
| 318 | |
| 319 | return $str; |
| 320 | } |
| 321 | |
| 322 | // ------------------------------------------------------------------------ |
| 323 | |
| 324 | /** |
| 325 | * Word Wrap |
| 326 | * |
| 327 | * Wraps text at the specified character. Maintains the integrity of words. |
| 328 | * |
| 329 | * @access public |
| 330 | * @param string the text string |
| 331 | * @param integer the number of characters to wrap at |
| 332 | * @return string |
| 333 | */ |
| 334 | function word_wrap($str, $chars = '76') |
| 335 | { |
admin | 1cf89aa | 2006-09-03 18:24:39 +0000 | [diff] [blame] | 336 | if ( ! is_numeric($chars)) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 337 | $chars = 76; |
| 338 | |
| 339 | $str = preg_replace("/(\r\n|\r|\n)/", "\n", $str); |
| 340 | $lines = split("\n", $str); |
| 341 | |
| 342 | $output = ""; |
| 343 | while (list(, $thisline) = each($lines)) |
| 344 | { |
| 345 | if (strlen($thisline) > $chars) |
| 346 | { |
| 347 | $line = ""; |
| 348 | $words = split(" ", $thisline); |
| 349 | while(list(, $thisword) = each($words)) |
| 350 | { |
| 351 | while((strlen($thisword)) > $chars) |
| 352 | { |
| 353 | $cur_pos = 0; |
| 354 | for($i=0; $i < $chars - 1; $i++) |
| 355 | { |
| 356 | $output .= $thisword[$i]; |
| 357 | $cur_pos++; |
| 358 | } |
| 359 | |
| 360 | $output .= "\n"; |
| 361 | $thisword = substr($thisword, $cur_pos, (strlen($thisword) - $cur_pos)); |
| 362 | } |
| 363 | |
| 364 | if ((strlen($line) + strlen($thisword)) > $chars) |
| 365 | { |
| 366 | $output .= $line."\n"; |
| 367 | $line = $thisword." "; |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | $line .= $thisword." "; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | $output .= $line."\n"; |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | $output .= $thisline."\n"; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | return $output; |
| 384 | } |
| 385 | |
| 386 | ?> |