00001 <?php
00002 define("STRING_SANITIZE_NONE", 0);
00003 define("STRING_SANITIZE_DB", 1);
00004 define("STRING_SANITIZE_HTML", 2);
00005
00006
00007
00008
00009
00010
00011
00012 class String {
00013 const HTML = 'html';
00014 const XML = 'xml';
00015
00016 public static $impl;
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 public static function clear_html($val) {
00028 return htmlspecialchars(strip_tags($val), ENT_QUOTES, GyroLocale::get_charset());
00029 }
00030
00031
00032
00033
00034
00035
00036
00037 public static function escape($val, $target = self::HTML) {
00038 if ($target === self::HTML) {
00039 return htmlentities(trim($val), ENT_QUOTES, GyroLocale::get_charset());
00040 }
00041 else {
00042 return htmlspecialchars(trim($val), ENT_QUOTES, GyroLocale::get_charset());
00043 }
00044 }
00045
00046
00047
00048
00049
00050
00051
00052 public static function unescape($val) {
00053 return html_entity_decode(trim($val), ENT_QUOTES, GyroLocale::get_charset());
00054 }
00055
00056
00057
00058
00059
00060
00061
00062
00063 public static function check_encoding($value, $encoding = false) {
00064 return self::$impl->check_encoding($value, $encoding);
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 public static function convert($value, $from = false, $to = false) {
00081 return self::$impl->convert($value, $from, $to);
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091 public static function currency($dbl, $includeUnit = true) {
00092 $locale_info = localeconv();
00093 $thousands_sep = Arr::get_item($locale_info, 'mon_thousands_sep', null);
00094 if (empty($thousands_sep)) {
00095 $thousands_sep = Arr::get_item($locale_info, 'thousands_sep', ',');
00096 }
00097 $decimal_point = Arr::get_item($locale_info, 'mon_decimal_point', null);
00098 if (empty($decimal_sep)) {
00099 $decimal_point = Arr::get_item($locale_info, 'decimal_point', '.');
00100 }
00101 $ret = number_format($dbl, 2, $decimal_point, $thousands_sep);
00102
00103 if ($includeUnit) {
00104 $currency_symbol = Arr::get_item($locale_info, 'currency_symbol', '$');
00105 $p_cs_precedes = Arr::get_item($locale_info, 'p_cs_precedes', true);
00106 $p_sep_by_space = Arr::get_item($locale_info, 'p_sep_by_space', true);
00107
00108 if ($p_cs_precedes) {
00109 if ($p_sep_by_space) {
00110 $currency_symbol .= ' ';
00111 }
00112 $ret = $currency_symbol . $ret;
00113 }
00114 else {
00115 if ($p_sep_by_space) {
00116 $currency_symbol = ' ' . $currency_symbol;
00117 }
00118 $ret .= $currency_symbol;
00119 }
00120 }
00121 return $ret;
00122 }
00123
00124
00125
00126
00127
00128
00129
00130 public static function int($int) {
00131 $int = Cast::int($int);
00132 if ($int < 10000) {
00133 return (string)$int;
00134 }
00135 else {
00136 return String::number($int, 0);
00137 }
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 public static function number($number, $decimals = 2, $system = false) {
00149 $locale_info = ($system) ? false : localeconv();
00150 $thousands_sep = ($system) ? '' : Arr::get_item($locale_info, 'thousands_sep', ',');
00151 $decimal_point = ($system) ? '.' : Arr::get_item($locale_info, 'decimal_point', '.');
00152 return number_format(Cast::float($number), $decimals, $decimal_point, $thousands_sep);
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 public static function localize_number($val) {
00166
00167
00168 $locale_info = localeconv();
00169 $thousands_sep = Arr::get_item($locale_info, 'thousands_sep', ',');
00170 $decimal_point = Arr::get_item($locale_info, 'decimal_point', '.');
00171
00172 if ($decimal_point != '.') {
00173
00174 $val = str_replace(',', $thousands_sep, $val);
00175
00176 $val = str_replace('.', $decimal_point, $val);
00177 }
00178 return $val;
00179 }
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 public static function delocalize_number($val) {
00193
00194
00195 $locale_info = localeconv();
00196 $thousands_sep = Arr::get_item($locale_info, 'thousands_sep', ',');
00197 $decimal_point = Arr::get_item($locale_info, 'decimal_point', '.');
00198
00199 if ($decimal_point != '.') {
00200
00201 $val = str_replace($thousands_sep, '', $val);
00202
00203 $val = str_replace($decimal_point, '.', $val);
00204 }
00205 return $val;
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 public static function singular_plural($num, $singular, $plural, $none = false) {
00220 $plural = str_replace('%num', $num, $plural);
00221 switch ($num) {
00222 case 1:
00223 return $singular;
00224 case 0:
00225 return ($none !== false) ? $none : $plural;
00226 default:
00227 return $plural;
00228 }
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 public static function to_lower($val, $count = 0) {
00240 if ($count > 0) {
00241 return self::to_lower(self::substr($val, 0, $count)) . self::substr($val, $count);
00242 }
00243 else {
00244 return self::$impl->to_lower($val);
00245 }
00246 }
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256 public static function to_upper($val, $count = 0) {
00257 if ($count > 0) {
00258 return self::to_upper(self::substr($val, 0, $count)) . self::substr($val, $count);
00259 }
00260 else {
00261 return self::$impl->to_upper($val);
00262 }
00263 }
00264
00265
00266
00267
00268 public static function length($val) {
00269 return self::$impl->length($val);
00270 }
00271
00272 public static function strpos($haystack, $needle, $offset = NULL) {
00273 return self::$impl->strpos($haystack, $needle, $offset);
00274 }
00275
00276 public static function stripos($haystack, $needle, $offset = NULL) {
00277 return self::$impl->stripos($haystack, $needle, $offset);
00278 }
00279
00280 public static function strrpos($haystack, $needle) {
00281 return self::$impl->strrpos($haystack, $needle);
00282 }
00283
00284 public static function contains($haystack, $needle) {
00285 return (self::strpos($haystack, $needle) !== false);
00286 }
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300 public static function preg_replace($pattern, $replacement, $subject, $limit = -1, &$count = false) {
00301 self::apply_u_modifier($pattern);
00302 return preg_replace($pattern, $replacement, $subject, $limit, $count);
00303 }
00304
00305 public static function preg_replace_callback($pattern, $callback, $subject, $limit = -1, &$count = false) {
00306 self::apply_u_modifier($pattern);
00307 return preg_replace_callback($pattern, $callback, $subject, $limit, $count);
00308 }
00309
00310 public static function preg_match($pattern, $subject, &$matches = array(), $flags = 0, $offset = 0) {
00311 self::apply_u_modifier($pattern);
00312 return preg_match($pattern, $subject, $matches, $flags, $offset);
00313 }
00314
00315 public static function preg_match_all($pattern, $subject, &$matches = array(), $flags = 0, $offset = 0) {
00316 self::apply_u_modifier($pattern);
00317 return preg_match_all($pattern, $subject, $matches, $flags, $offset);
00318 }
00319
00320 public static function preg_split($pattern, $subject, $limit = -1, $flags = 0) {
00321 self::apply_u_modifier($pattern);
00322 return preg_split($pattern, $subject, $limit, $flags);
00323 }
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 private static function apply_u_modifier(&$pattern) {
00337 if (GyroLocale::get_charset() !== 'UTF-8') {
00338 return;
00339 }
00340
00341 if (!function_exists('_append_u_modifier')) {
00342
00343
00344
00345
00346
00347
00348 function _append_u_modifier($regex) {
00349 if (GyroLocale::get_charset() == 'UTF-8') {
00350 $regex = str_replace('\b{<}', '(?<!\w)', $regex);
00351 $regex = str_replace('\b{>}', '(?!\w)', $regex);
00352 $regex = str_replace('\b{<>}', '(?!\w)(?<!\w)', $regex);
00353 $regex = str_replace('\W', '[^\pL\pN]', $regex);
00354 $regex = str_replace('\w', '[\pL\pN]', $regex);
00355 $regex = str_replace('\s', '[\pZ]', $regex);
00356 $regex = str_replace('\S', '[\PZ]', $regex);
00357 $regex = str_replace('\d', '[\pN]', $regex);
00358 $regex = str_replace('\D', '[\PN]', $regex);
00359 $regex .= 'u';
00360 }
00361 else {
00362 $regex = str_replace('\b{<}', '\b', $regex);
00363 $regex = str_replace('\b{>}', '\b', $regex);
00364 $regex = str_replace('\b{<>}', '\b', $regex);
00365 }
00366
00367 return $regex;
00368 }
00369 }
00370
00371 if (is_array($pattern)) {
00372 $pattern = array_map('_append_u_modifier', $pattern);
00373 }
00374 else {
00375 $pattern = _append_u_modifier($pattern);
00376 }
00377 }
00378
00379
00380
00381
00382 public static function substr($val, $start = 0, $length = NULL) {
00383 return self::$impl->substr($val, $start, $length);
00384 }
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394 public static function substr_word($val, $start, $max_length, $elipsis = false) {
00395 $val .= ' ';
00396 $ret = self::substr($val, $start, $max_length);
00397 $pos = self::strrpos($ret, ' ');
00398 if ($pos === false) {
00399
00400
00401 $test = self::substr($val, $start + $max_length, 1);
00402 if ($test != '' && $test != ' ') {
00403 $ret = '';
00404 }
00405 }
00406 else {
00407 $ret = self::substr($ret, 0, $pos);
00408 }
00409 if ($elipsis && $ret) {
00410 $ret .= '...';
00411 }
00412
00413 return $ret;
00414 }
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429 public static function substr_sentence($val, $start, $max_length, $elipsis = false) {
00430 $val_temp = $val . ' ';
00431 $pos = false;
00432 $ret = self::substr($val_temp, $start, $max_length);
00433 $punctuations = array('?', '!', '.');
00434 foreach($punctuations as $punc) {
00435 $pos_temp = self::strrpos($ret, $punc);
00436
00437 if ($pos_temp !== false && $pos_temp > $pos) {
00438
00439
00440 $test = self::substr($val_temp, $pos_temp + 1, 1);
00441 if ($test === ' ' || $test === '') {
00442 $pos = $pos_temp;
00443 }
00444 }
00445 }
00446 if ($pos === false) {
00447
00448 $test = self::substr($val_temp, $start + $max_length, 1);
00449 if (!in_array($test, $punctuations)) {
00450 $ret = self::substr_word($val, $start, $max_length, false);
00451 }
00452 }
00453 else {
00454 $ret = self::substr($ret, 0, $pos + 1);
00455 }
00456
00457 if ($elipsis && $ret) {
00458 $ret .= '...';
00459 }
00460
00461 return $ret;
00462 }
00463
00464 public static function right($val, $count) {
00465 return self::substr($val, -$count, $count);
00466 }
00467
00468 public static function left($val, $count) {
00469 return self::substr($val, 0, $count);
00470 }
00471
00472
00473
00474
00475
00476
00477 public static function starts_with($haystack, $needle) {
00478 if ($needle !== '') {
00479 return (strncmp($haystack, $needle, strlen($needle)) == 0);
00480 }
00481 return false;
00482
00483
00484
00485
00486
00487
00488
00489
00490 }
00491
00492
00493
00494
00495 public static function ends_with($haystack, $needle) {
00496 $lenght_needle = self::length($needle);
00497 if ($lenght_needle > 0) {
00498 return self::substr($haystack, -$lenght_needle, $lenght_needle) == $needle;
00499 }
00500 return false;
00501 }
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511 public static function plain_ascii($path, $separator = '-', $removewhitespace = true) {
00512
00513 $ret = rawurldecode($path);
00514 $ret = html_entity_decode($ret, ENT_QUOTES, GyroLocale::get_charset());
00515 $ret = strip_tags($ret);
00516 if ($removewhitespace) {
00517 $ret = self::$impl->to_lower($ret);
00518 $replace = array(
00519 'ä' => 'ae', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'å' => 'a', 'æ' => 'ae',
00520 'ç' => 'c', 'ć' => 'c', 'ĉ' => 'c', 'č' => 'c',
00521 'ö' => 'oe','ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ø' => 'oe',
00522 'ü' => 'ue', 'ù' => 'u', 'ú' => 'u', 'û' => 'u',
00523 'ß' => 'ss',
00524 'é' => 'e', 'è' => 'e', 'ê' => 'e', 'ë' => 'e',
00525 'ý' => 'y',
00526 'ñ' => 'n',
00527 'î' => 'i', 'ì' => 'i', 'í' => 'i', 'ï' => 'i'
00528 );
00529 $ret = strtr($ret, $replace);
00530
00531 $pattern = str_replace('%sep%', $separator, '*[^a-zA-Z0-9_%sep%]+*');
00532 $ret = preg_replace($pattern, $separator, $ret);
00533
00534
00535 if ($separator != '') {
00536 $test = $separator . $separator;
00537 while(strpos($ret, $test) !== false) {
00538 $ret = str_replace($test, $separator, $ret);
00539 }
00540 }
00541 $ret = trim($ret, $separator);
00542 }
00543 return $ret;
00544 }
00545
00546
00547
00548
00549
00550
00551 public static function extract_before($haystack, $needle) {
00552 $pos = strpos($haystack, $needle);
00553 $ret = $haystack;
00554 if ($pos !== false) {
00555 $ret = self::substr($haystack, 0, $pos);
00556 }
00557 return $ret;
00558 }
00559
00560
00561
00562
00563
00564
00565 public static function extract_after($haystack, $needle) {
00566 $pos = strpos($haystack, $needle);
00567 $ret = $haystack;
00568 if ($pos !== false) {
00569 $pos += self::length($needle);
00570 $ret = self::substr($haystack, $pos);
00571 }
00572 return $ret;
00573 }
00574
00575
00576
00577
00578
00579
00580
00581 public static function explode_terms($term) {
00582
00583 $terms = explode(' ', $term);
00584 $args = array();
00585 $arg = '';
00586 $isQuoted = false;
00587
00588 foreach ($terms as $thisTerm) {
00589 $thisTerm = trim($thisTerm);
00590 $appendix = '';
00591 if ( $thisTerm === '') {
00592 continue;
00593 }
00594
00595 if ($isQuoted === false) {
00596 $arg = '';
00597
00598 if ( self::substr($thisTerm, 0, 1) === '"') {
00599 $isQuoted = true;
00600 $arg = '"';
00601 $thisTerm = self::substr($thisTerm, 1);
00602 }
00603 }
00604 if ($isQuoted === true) {
00605 $appendix = ' ';
00606
00607
00608 if ( self::substr($thisTerm, -1) === '"') {
00609 $isQuoted = false;
00610 $thisTerm = self::substr($thisTerm, 0, strlen($thisTerm) - 1);
00611 $appendix = '"';
00612 }
00613 }
00614 else {
00615
00616
00617
00618 $thisTerm = self::preg_replace('|(\S)\W|', '\1 ', $thisTerm);
00619 $thisTerms = explode(' ', $thisTerm);
00620 $lastIndex = count($thisTerms) - 1;
00621 for($i = 0; $i < $lastIndex; $i++) {
00622 $args[] = $thisTerms[$i];
00623 }
00624 $thisTerm = $thisTerms[$lastIndex];
00625 }
00626
00627 $arg .= $thisTerm . $appendix;
00628 if ($isQuoted === false) {
00629 $args[] = $arg;
00630 }
00631 }
00632
00633 $args = array_map('trim', $args);
00634 for ($i = count($args) - 1; $i >= 0; $i--) {
00635 if ($args[$i] === '') {
00636 unset($args[$i]);
00637 }
00638 }
00639
00640 return $args;
00641 }
00642 }
00643
00644 if (function_exists('mb_detect_encoding')) {
00645 require_once dirname(__FILE__) . '/string_impl/string.mbstring.cls.php';
00646 String::$impl = new StringMBString();
00647 }
00648 else {
00649 require_once dirname(__FILE__) . '/string_impl/string.php.cls.php';
00650 String::$impl = new StringPHP();
00651 }