123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- class StringHelper {
- private static $TRUE_VALUES = array("on", "true", "t", "yes");
- private static $FALSE_VALUES = array("off", "false", "f", "no");
-
-
- public static function multiply($strings, $tokens, $replacements) {
- $strings = (array) $strings;
- $results = array();
- foreach ($strings as $string) {
- $results[] = str_replace($tokens, $replacements, $string);
- }
- return $results;
- }
-
- public static function unqualify($qualifiedName, $separator = '.') {
-
- $pos = strrpos($qualifiedName, $separator);
- if ($pos === false) {
- return $qualifiedName;
- } else {
- return substr($qualifiedName, $pos + 1);
- }
- }
-
- public static function toCharArray($str) {
- $ret=array();
- $len=strlen($str);
- for ($i=0; $i < $len; $i++) {
- $ret[] = $str{$i};
- }
- return $ret;
- }
-
-
-
- public static function qualifier($qualifiedName, $seperator = '.') {
- $pos = strrchr($qualifiedName, $seperator);
- if ($pos === false) {
- return '';
- } else {
- return substr($qualifiedName, 0, $pos);
- }
- }
-
-
-
- public static function prefix( $columns, $prefix) {
- if ($prefix == null) return $columns;
- $qualified = array();
- foreach($columns as $key => $column) {
- $qualified[$key] = $prefix . $column;
- }
- return $qualified;
- }
-
-
-
- public static function root($qualifiedName, $separator = '.') {
- $loc = strpos($qualifiedName, $separator);
- return ($loc === false) ? $qualifiedName : substr($qualifiedName, 0, $loc);
- }
-
-
- public static function hashCode($string) {
- return crc32($string);
- }
-
-
-
- public static function booleanValue($s) {
- if (is_bool($s)) {
- return $s;
- }
-
- $trimmed = strtolower(trim($s));
- return (boolean) in_array($trimmed, self::$TRUE_VALUES);
- }
-
- public static function isBoolean($s) {
- if (is_bool($s)) {
- return true;
- }
-
- if ($s === "" || $s === null || !is_string($s)) {
- return false;
- }
- $test = trim(strtolower($s));
- return (boolean) in_array($test, array_merge(self::$FALSE_VALUES, self::$TRUE_VALUES));
- }
-
-
- public static function key() {
- $args = func_get_args();
- return serialize($args);
- }
-
-
- public static function startsWith($check, $string) {
- if ($check === "" || $check === $string) {
- return true;
- } else {
- return (strpos($string, $check) === 0) ? true : false;
- }
- }
-
-
- public static function endsWith($check, $string) {
- if ($check === "" || $check === $string) {
- return true;
- } else {
- return (strpos(strrev($string), strrev($check)) === 0) ? true : false;
- }
- }
-
- public static function substring($string, $startpos, $endpos = -1) {
- $len = strlen($string);
- $endpos = (int) (($endpos === -1) ? $len-1 : $endpos);
- if ($startpos > $len-1 || $startpos < 0) {
- trigger_error("substring(), Startindex out of bounds must be 0<n<$len", E_USER_ERROR);
- }
- if ($endpos > $len-1 || $endpos < $startpos) {
- trigger_error("substring(), Endindex out of bounds must be $startpos<n<".($len-1), E_USER_ERROR);
- }
- if ($startpos === $endpos) {
- return (string) $string{$startpos};
- } else {
- $len = $endpos-$startpos;
- }
- return substr($string, $startpos, $len+1);
- }
-
- public static function isSlotVar($value) {
- $value = trim($value);
- if ($value === "") return false;
- return preg_match('/^%\{([\w\.\-]+)\}$/', $value);
- }
-
-
- public static function slotVar($var) {
- return trim($var, '%{} ');
- }
-
- }
|