00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 if ( ! function_exists('word_limiter'))
00042 {
00043 function word_limiter($str, $limit = 100, $end_char = '…')
00044 {
00045 if (trim($str) == '')
00046 {
00047 return $str;
00048 }
00049
00050 preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
00051
00052 if (strlen($str) == strlen($matches[0]))
00053 {
00054 $end_char = '';
00055 }
00056
00057 return rtrim($matches[0]).$end_char;
00058 }
00059 }
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 if ( ! function_exists('character_limiter'))
00076 {
00077 function character_limiter($str, $n = 500, $end_char = '…')
00078 {
00079 if (strlen($str) < $n)
00080 {
00081 return $str;
00082 }
00083
00084 $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
00085
00086 if (strlen($str) <= $n)
00087 {
00088 return $str;
00089 }
00090
00091 $out = "";
00092 foreach (explode(' ', trim($str)) as $val)
00093 {
00094 $out .= $val.' ';
00095 if (strlen($out) >= $n)
00096 {
00097 return trim($out).$end_char;
00098 }
00099 }
00100 }
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 if ( ! function_exists('ascii_to_entities'))
00115 {
00116 function ascii_to_entities($str)
00117 {
00118 $count = 1;
00119 $out = '';
00120 $temp = array();
00121
00122 for ($i = 0, $s = strlen($str); $i < $s; $i++)
00123 {
00124 $ordinal = ord($str[$i]);
00125
00126 if ($ordinal < 128)
00127 {
00128 $out .= $str[$i];
00129 }
00130 else
00131 {
00132 if (count($temp) == 0)
00133 {
00134 $count = ($ordinal < 224) ? 2 : 3;
00135 }
00136
00137 $temp[] = $ordinal;
00138
00139 if (count($temp) == $count)
00140 {
00141 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
00142
00143 $out .= '&#'.$number.';';
00144 $count = 1;
00145 $temp = array();
00146 }
00147 }
00148 }
00149
00150 return $out;
00151 }
00152 }
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 if ( ! function_exists('entities_to_ascii'))
00167 {
00168 function entities_to_ascii($str, $all = TRUE)
00169 {
00170 if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
00171 {
00172 for ($i = 0, $s = count($matches['0']); $i < $s; $i++)
00173 {
00174 $digits = $matches['1'][$i];
00175
00176 $out = '';
00177
00178 if ($digits < 128)
00179 {
00180 $out .= chr($digits);
00181
00182 }
00183 elseif ($digits < 2048)
00184 {
00185 $out .= chr(192 + (($digits - ($digits % 64)) / 64));
00186 $out .= chr(128 + ($digits % 64));
00187 }
00188 else
00189 {
00190 $out .= chr(224 + (($digits - ($digits % 4096)) / 4096));
00191 $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64));
00192 $out .= chr(128 + ($digits % 64));
00193 }
00194
00195 $str = str_replace($matches['0'][$i], $out, $str);
00196 }
00197 }
00198
00199 if ($all)
00200 {
00201 $str = str_replace(array("&", "<", ">", """, "'", "-"),
00202 array("&","<",">","\"", "'", "-"),
00203 $str);
00204 }
00205
00206 return $str;
00207 }
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 if ( ! function_exists('word_censor'))
00226 {
00227 function word_censor($str, $censored, $replacement = '')
00228 {
00229 if ( ! is_array($censored))
00230 {
00231 return $str;
00232 }
00233
00234 $str = ' '.$str.' ';
00235 foreach ($censored as $badword)
00236 {
00237 if ($replacement != '')
00238 {
00239 $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str);
00240 }
00241 else
00242 {
00243 $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str);
00244 }
00245 }
00246
00247 return trim($str);
00248 }
00249 }
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 if ( ! function_exists('highlight_code'))
00263 {
00264 function highlight_code($str)
00265 {
00266
00267
00268 $str = str_replace(array('<', '>'), array('<', '>'), $str);
00269
00270
00271
00272
00273 $str = str_replace(array('<?', '?>', '<%', '%>', '\\', '</script>'),
00274 array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'), $str);
00275
00276
00277
00278
00279
00280
00281 $str = '<?php tempstart'."\n".$str.'tempend ?>';
00282
00283
00284 $str = highlight_string($str, TRUE);
00285
00286
00287
00288 if (abs(phpversion()) < 5)
00289 {
00290 $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
00291 $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
00292 }
00293
00294
00295 $str = preg_replace("#<code>.+?tempstart<br />(?:</span>)?#is", "<code>\n", $str);
00296 $str = preg_replace("#tempend.+#is", "</span>\n</code>", $str);
00297
00298
00299 $str = str_replace(array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
00300 array('<?', '?>', '<%', '%>', '\\', '</script>'), $str);
00301
00302 return $str;
00303 }
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 if ( ! function_exists('highlight_phrase'))
00321 {
00322 function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
00323 {
00324 if ($str == '')
00325 {
00326 return '';
00327 }
00328
00329 if ($phrase != '')
00330 {
00331 return preg_replace('/('.preg_quote($phrase, '/').')/i', $tag_open."\\1".$tag_close, $str);
00332 }
00333
00334 return $str;
00335 }
00336 }
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 if ( ! function_exists('word_wrap'))
00353 {
00354 function word_wrap($str, $charlim = '76')
00355 {
00356
00357 if ( ! is_numeric($charlim))
00358 $charlim = 76;
00359
00360
00361 $str = preg_replace("| +|", " ", $str);
00362
00363
00364 if (strpos($str, "\r") !== FALSE)
00365 {
00366 $str = str_replace(array("\r\n", "\r"), "\n", $str);
00367 }
00368
00369
00370
00371 $unwrap = array();
00372 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
00373 {
00374 for ($i = 0; $i < count($matches['0']); $i++)
00375 {
00376 $unwrap[] = $matches['1'][$i];
00377 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
00378 }
00379 }
00380
00381
00382
00383
00384 $str = wordwrap($str, $charlim, "\n", FALSE);
00385
00386
00387 $output = "";
00388 foreach (explode("\n", $str) as $line)
00389 {
00390
00391
00392 if (strlen($line) <= $charlim)
00393 {
00394 $output .= $line."\n";
00395 continue;
00396 }
00397
00398 $temp = '';
00399 while((strlen($line)) > $charlim)
00400 {
00401
00402 if (preg_match("!\[url.+\]|://|wwww.!", $line))
00403 {
00404 break;
00405 }
00406
00407
00408 $temp .= substr($line, 0, $charlim-1);
00409 $line = substr($line, $charlim-1);
00410 }
00411
00412
00413
00414 if ($temp != '')
00415 {
00416 $output .= $temp . "\n" . $line;
00417 }
00418 else
00419 {
00420 $output .= $line;
00421 }
00422
00423 $output .= "\n";
00424 }
00425
00426
00427 if (count($unwrap) > 0)
00428 {
00429 foreach ($unwrap as $key => $val)
00430 {
00431 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
00432 }
00433 }
00434
00435
00436 $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
00437
00438 return $output;
00439 }
00440 }
00441
00442
00443
00444