String Class Reference
[Lib]
Wraps string functions, calls mb_ functions, if available. More...
Static Public Member Functions |
|
static | check_encoding ($value, $encoding=false) |
Check if given string matches current
encoding. |
|
static | clear_html ($val) |
Static. |
|
static | contains ($haystack, $needle) |
static | convert ($value, $from=false, $to=false) |
Convert input charset. |
|
static | currency ($dbl, $includeUnit=true) |
Static. |
|
static | delocalize_number ($val) |
Turns a string representing a number into a
string representing a number in C locale. |
|
static | ends_with ($haystack, $needle) |
Returns true if haystack starts with
needlse. |
|
static | escape ($val, $target=self::HTML) |
Static. |
|
static | explode_terms ($term) |
Extracts arguments from given string.
|
|
static | extract_after ($haystack, $needle) |
Returns part of haystack that is after
needle. |
|
static | extract_before ($haystack, $needle) |
Returns part of haystack that is before
needle. |
|
static | int ($int) |
Static. |
|
static | left ($val, $count) |
static | length ($val) |
Character set aware strlen(). |
|
static | localize_number ($val) |
Turns a string representing a number into a
string representing a number in current locale. |
|
static | number ($number, $decimals=2, $system=false) |
Static. |
|
static | plain_ascii ($path, $separator= '-', $removewhitespace=true) |
Removes everything that is not plain ascii
and replaces known special chars like umlauts. |
|
static | preg_match ($pattern, $subject, &$matches=array(), $flags=0, $offset=0) |
static | preg_match_all ($pattern, $subject, &$matches=array(), $flags=0, $offset=0) |
static | preg_replace ($pattern, $replacement, $subject, $limit=-1, &$count=false) |
A unicode aware implementation of
preg_replace. |
|
static | preg_replace_callback ($pattern, $callback, $subject, $limit=-1, &$count=false) |
static | preg_split ($pattern, $subject, $limit=-1, $flags=0) |
static | right ($val, $count) |
static | singular_plural ($num, $singular, $plural, $none=false) |
Return string depending on value of $num.
|
|
static | starts_with ($haystack, $needle) |
Returns true if haystack starts with
needlse. |
|
static | stripos ($haystack, $needle, $offset=NULL) |
static | strpos ($haystack, $needle, $offset=NULL) |
static | strrpos ($haystack, $needle) |
static | substr ($val, $start=0, $length=NULL) |
Character set aware substr. |
|
static | substr_sentence ($val, $start, $max_length, $elipsis=false) |
Get substr, but respect sentence boundaries.
|
|
static | substr_word ($val, $start, $max_length, $elipsis=false) |
Get substr, but respect word boundaries.
|
|
static | to_lower ($val, $count=0) |
Character set aware strtolower(). |
|
static | to_upper ($val, $count=0) |
Character set aware strtoupper(). |
|
static | unescape ($val) |
Static. |
|
Public Attributes |
|
const | HTML = 'html' |
const | XML = 'xml' |
Static Public Attributes |
|
static | $impl |
Detailed Description
Wraps string functions, calls mb_ functions, if available.
Definition at line 12 of file string.cls.php.
Member Function Documentation
static String::check_encoding | ( | $ | value, | |
$ | encoding = false |
|||
) | [static] |
Check if given string matches current encoding.
- Parameters:
-
string $value Value to check string $encoding Encoding to check against. Use FALSE for current encoding
- Returns:
- bool
Definition at line 63 of file string.cls.php.
static String::clear_html | ( | $ | val | ) | [static] |
Static.
processes a string to strip of HTML and quotes. Avoids injection attacks
- Attention:
- Content is escaped after stripping tags, so you should not call this function just to strip tags. Use it to clean user input.
- Parameters:
-
String The text to process
- Returns:
- String The cleaned text
Definition at line 27 of file string.cls.php.
00027 { 00028 return htmlspecialchars(strip_tags($val), ENT_QUOTES, GyroLocale::get_charset()); 00029 }
static String::contains | ( | $ | haystack, | |
$ | needle | |||
) | [static] |
Definition at line 284 of file string.cls.php.
Convert input charset.
- Attention:
- Note that charset autodetection usually requries the according charsets to be installed on your system. If you use UTF-8, e.g. a UTF-8 locale should be installed. While on Windows, this is usually the case, you may want to check this on Linux by invoking "locale -a" on the command line.
- Parameters:
-
string $value Input to convert string $from Charset to convert from. If empty, system tries to autodetect it (may fail, though) string $to Charset to convert to, if empty charset set on GyroLocale is used
- Returns:
- string
Definition at line 80 of file string.cls.php.
static String::currency | ( | $ | dbl, | |
$ | includeUnit = true |
|||
) | [static] |
Static.
Convert float value into a currency string according to locale settings
- Parameters:
-
float The numeric value bool True to include unit, false to obey
- Returns:
- String The formatted string, e.g. "$10.20"
Definition at line 91 of file string.cls.php.
00091 { 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 }
static String::delocalize_number | ( | $ | val | ) | [static] |
Turns a string representing a number into a string representing a number in C locale.
Example: In european countries decimal sep is a comma, not a dot, so while user enters 192,123 this mus be transformed to 192.123 so it can be understand correctly by PHP , DB or else
Be aware to not pass a number to this function twice!
- Parameters:
-
string $val string
Definition at line 192 of file string.cls.php.
00192 { 00193 // Convert language specific float value to C float 00194 // e.g. in Germany: 1.000,30 to 1000.30 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 // Strip thousands sep 00201 $val = str_replace($thousands_sep, '', $val); 00202 // Convert decimal sep 00203 $val = str_replace($decimal_point, '.', $val); 00204 } 00205 return $val; 00206 }
static String::ends_with | ( | $ | haystack, | |
$ | needle | |||
) | [static] |
Returns true if haystack starts with needlse.
Definition at line 495 of file string.cls.php.
00495 { 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 }
static String::escape | ( | $ | val, | |
$ | target =
self::HTML |
|||
) | [static] |
Static.
Preprocesses a string to not contain "<", ">" and other special chars
- Parameters:
-
String The text to process
- Returns:
- String The cleaned text
Definition at line 37 of file string.cls.php.
00037 { 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 }
static String::explode_terms | ( | $ | term | ) | [static] |
Extracts arguments from given string.
Arguments are seperated by white space, but everything quoted by " is regarded as one argumne
- Returns:
- array
Definition at line 581 of file string.cls.php.
00581 { 00582 // split into array 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 // We do not append, so check if term starts with " 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 // Do not make this an else-branch! Really! Don't! 00607 // if string ends with ", appending is over 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 // We have a non-quoted term. Check if there are non-character words in it 00616 // replace non-word characters with space, if there is no space before them 00617 // E.g. 'Saint-John-Cathedral -London' to 'Saint John Cathedral -London' 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 }
static String::extract_after | ( | $ | haystack, | |
$ | needle | |||
) | [static] |
Returns part of haystack that is after needle.
If needle is not found, $haystack is returned
Definition at line 565 of file string.cls.php.
00565 { 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 }
static String::extract_before | ( | $ | haystack, | |
$ | needle | |||
) | [static] |
Returns part of haystack that is before needle.
If needle is not found, $haystack is returned
Definition at line 551 of file string.cls.php.
00551 { 00552 $pos = strpos($haystack, $needle); 00553 $ret = $haystack; 00554 if ($pos !== false) { 00555 $ret = self::substr($haystack, 0, $pos); 00556 } 00557 return $ret; 00558 }
static String::int | ( | $ | int | ) | [static] |
Static.
Convert integer value into a string according to locale settings
- Parameters:
-
int The numeric value
- Returns:
- String The formatted string, e.g. "10,200"
Definition at line 130 of file string.cls.php.
00130 { 00131 $int = Cast::int($int); 00132 if ($int < 10000) { 00133 return (string)$int; 00134 } 00135 else { 00136 return String::number($int, 0); 00137 } 00138 }
static String::left | ( | $ | val, | |
$ | count | |||
) | [static] |
Definition at line 468 of file string.cls.php.
00468 { 00469 return self::substr($val, 0, $count); 00470 }
static String::length | ( | $ | val | ) | [static] |
Character set aware strlen().
Definition at line 268 of file string.cls.php.
static String::localize_number | ( | $ | val | ) | [static] |
Turns a string representing a number into a string representing a number in current locale.
Example: In european countries decimal sep is a comma, so this will turn '2.45' into '2,45'
Be aware to not pass a number to this function twice!
- Parameters:
-
string $val string
Definition at line 165 of file string.cls.php.
00165 { 00166 // Convert C specific float value to locale float 00167 // e.g. in Germany: 1,000.30 to 1.000,30 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 // Strip thousands sep 00174 $val = str_replace(',', $thousands_sep, $val); 00175 // Convert decimal sep 00176 $val = str_replace('.', $decimal_point, $val); 00177 } 00178 return $val; 00179 }
static String::number | ( | $ | number, | |
$ | decimals = 2 , |
|||
$ | system = false |
|||
) | [static] |
Static.
Convert numeric value into a string according to locale settings
- Parameters:
-
mixed The numeric value int Number of decimals boolean If true, C formatting is used
- Returns:
- String The formatted string, e.g. "10,200.67"
Definition at line 148 of file string.cls.php.
00148 { 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 }
static String::plain_ascii | ( | $ | path, | |
$ | separator = '-' , |
|||
$ | removewhitespace = true |
|||
) | [static] |
Removes everything that is not plain ascii and replaces known special chars like umlauts.
Text is converted to lower case, too
- Parameters:
-
string text to clean string if not empty, everything that is not a letter or number is replaced by this
Definition at line 511 of file string.cls.php.
00511 { 00512 // Away with html specific stuff 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 // remove duplicated seps, like "Tom & Jerry" => "Tom---Jerry" 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 }
static String::preg_match | ( | $ | pattern, | |
$ | subject, | |||
&$ | matches = array() , |
|||
$ | flags = 0 , |
|||
$ | offset = 0 |
|||
) | [static] |
Definition at line 310 of file string.cls.php.
00310 { 00311 self::apply_u_modifier($pattern); 00312 return preg_match($pattern, $subject, $matches, $flags, $offset); 00313 }
static String::preg_match_all | ( | $ | pattern, | |
$ | subject, | |||
&$ | matches = array() , |
|||
$ | flags = 0 , |
|||
$ | offset = 0 |
|||
) | [static] |
Definition at line 315 of file string.cls.php.
00315 { 00316 self::apply_u_modifier($pattern); 00317 return preg_match_all($pattern, $subject, $matches, $flags, $offset); 00318 }
static String::preg_replace | ( | $ | pattern, | |
$ | replacement, | |||
$ | subject, | |||
$ | limit = -1 , |
|||
&$ | count = false |
|||
) | [static] |
A unicode aware implementation of preg_replace.
See apply_u_modifier() for a list of supported types and assertions
- Parameters:
-
string $pattern The patter to search for string $replacement The string to replace with string $subject The text to search integer $limit The number of replacements to do integer $count Filled with number of replacements
- Returns:
- integer Number of replacements
Definition at line 300 of file string.cls.php.
00300 { 00301 self::apply_u_modifier($pattern); 00302 return preg_replace($pattern, $replacement, $subject, $limit, $count); 00303 }
static String::preg_replace_callback | ( | $ | pattern, | |
$ | callback, | |||
$ | subject, | |||
$ | limit = -1 , |
|||
&$ | count = false |
|||
) | [static] |
Definition at line 305 of file string.cls.php.
00305 { 00306 self::apply_u_modifier($pattern); 00307 return preg_replace_callback($pattern, $callback, $subject, $limit, $count); 00308 }
static String::preg_split | ( | $ | pattern, | |
$ | subject, | |||
$ | limit = -1 , |
|||
$ | flags = 0 |
|||
) | [static] |
Definition at line 320 of file string.cls.php.
00320 { 00321 self::apply_u_modifier($pattern); 00322 return preg_split($pattern, $subject, $limit, $flags); 00323 }
static String::right | ( | $ | val, | |
$ | count | |||
) | [static] |
Definition at line 464 of file string.cls.php.
00464 { 00465 return self::substr($val, -$count, $count); 00466 }
static String::singular_plural | ( | $ | num, | |
$ | singular, | |||
$ | plural, | |||
$ | none = false |
|||
) | [static] |
Return string depending on value of $num.
If $num is 1, $singuar is returned If $num is not 1, $plural is returned (num gets replaced by $num) If $num is 0, and $none is not FALSE, $none is returned, else $plural is returned (num gets replaced by $num)
- Since:
- 0.5.1
- Returns:
- string
Definition at line 219 of file string.cls.php.
static String::starts_with | ( | $ | haystack, | |
$ | needle | |||
) | [static] |
Returns true if haystack starts with needlse.
- Attention:
- If needle is en empty string, this function returns false!
Definition at line 477 of file string.cls.php.
00477 { 00478 if ($needle !== '') { 00479 return (strncmp($haystack, $needle, strlen($needle)) == 0); 00480 } 00481 return false; 00482 /* 00483 if ($needle !== '') { 00484 return self::substr($haystack, 0, self::length($needle)) == $needle; 00485 } 00486 else { 00487 return false; 00488 } 00489 */ 00490 }
static String::stripos | ( | $ | haystack, | |
$ | needle, | |||
$ | offset = NULL |
|||
) | [static] |
Definition at line 276 of file string.cls.php.
static String::strpos | ( | $ | haystack, | |
$ | needle, | |||
$ | offset = NULL |
|||
) | [static] |
Definition at line 272 of file string.cls.php.
static String::strrpos | ( | $ | haystack, | |
$ | needle | |||
) | [static] |
Definition at line 280 of file string.cls.php.
static String::substr | ( | $ | val, | |
$ | start = 0 , |
|||
$ | length = NULL |
|||
) | [static] |
Character set aware substr.
Definition at line 382 of file string.cls.php.
static String::substr_sentence | ( | $ | val, | |
$ | start, | |||
$ | max_length, | |||
$ | elipsis = false |
|||
) | [static] |
Get substr, but respect sentence boundaries.
Calls substr_word, if no sentence ends within given range.
- Attention:
- This function in general is rather stupid. Should recognize if a dot is used within sentence, e.g. within an URL or as thousands seperator, but will fail on stuff like "etc.".
- Parameters:
-
string $val int $start Start of substr (usually 0) int $max_length Maximum length of substring bool $elipsis Append "..." to the string
Definition at line 429 of file string.cls.php.
00429 { 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 //var_dump($ret, $punc, $pos_temp); 00437 if ($pos_temp !== false && $pos_temp > $pos) { 00438 // There is a punctuation character and it seems to be the last (up to now), 00439 // so check if there is a space following it 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 // Check if a punctuation follows substring 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 }
static String::substr_word | ( | $ | val, | |
$ | start, | |||
$ | max_length, | |||
$ | elipsis = false |
|||
) | [static] |
Get substr, but respect word boundaries.
- Parameters:
-
string $val int $start Start of substr (usually 0) int $max_length Maximum length of substring bool $elipsis Append "..." to the string
Definition at line 394 of file string.cls.php.
00394 { 00395 $val .= ' '; 00396 $ret = self::substr($val, $start, $max_length); 00397 $pos = self::strrpos($ret, ' '); 00398 if ($pos === false) { 00399 // No space found in given substr. 00400 // Check if a space follows substring 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 }
static String::to_lower | ( | $ | val, | |
$ | count = 0 |
|||
) | [static] |
Character set aware strtolower().
- Parameters:
-
String Value to convert into lowercase Integer Number of chars to convert, 0 for all.
- Returns:
- String converted string
Definition at line 239 of file string.cls.php.
00239 { 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 }
static String::to_upper | ( | $ | val, | |
$ | count = 0 |
|||
) | [static] |
Character set aware strtoupper().
- Parameters:
-
String Value to convert into lowercase Integer Number of chars to convert, 0 for all.
- Returns:
- String converted string
Definition at line 256 of file string.cls.php.
00256 { 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 }
static String::unescape | ( | $ | val | ) | [static] |
Static.
Preprocesses a string so > and similar get transformed to real characte4re
- Parameters:
-
String The text to process
- Returns:
- String The cleaned text
Definition at line 52 of file string.cls.php.
00052 { 00053 return html_entity_decode(trim($val), ENT_QUOTES, GyroLocale::get_charset()); 00054 }
Member Data Documentation
String::$impl [static] |
Definition at line 16 of file string.cls.php.
const String::HTML = 'html' |
Definition at line 13 of file string.cls.php.
const String::XML = 'xml' |
Definition at line 14 of file string.cls.php.
The documentation for this class was generated from the following file:
- gyro/core/lib/helpers/string.cls.php