TeamSpeak 3 PHP Framework  1.1.12
libraries/TeamSpeak3/Helper/String.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /**
00004  * @file
00005  * TeamSpeak 3 PHP Framework
00006  *
00007  * $Id: String.php 2/18/2012 12:42:46 scp@orilla $
00008  *
00009  * This program is free software: you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation, either version 3 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00021  *
00022  * @package   TeamSpeak3
00023  * @version   1.1.12
00024  * @author    Sven 'ScP' Paulsen
00025  * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
00026  */
00027 
00028 /**
00029  * @class TeamSpeak3_Helper_String
00030  * @brief Helper class for string handling.
00031  */
00032 class TeamSpeak3_Helper_String implements ArrayAccess, Iterator, Countable
00033 {
00034   /**
00035    * Stores the original string.
00036    *
00037    * @var string
00038    */
00039   protected $string;
00040 
00041   /**
00042    * @ignore
00043    */
00044   protected $position = 0;
00045 
00046   /**
00047    * The TeamSpeak3_Helper_String constructor.
00048    *
00049    * @param  string $string
00050    * @return TeamSpeak3_Helper_String
00051    */
00052   public function __construct($string)
00053   {
00054     $this->string = strval($string);
00055   }
00056 
00057   /**
00058    * Returns a TeamSpeak3_Helper_String object for thegiven string.
00059    *
00060    * @param  string $string
00061    * @return TeamSpeak3_Helper_String
00062    */
00063   public static function factory($string)
00064   {
00065     return new self($string);
00066   }
00067 
00068   /**
00069    * Replaces every occurrence of the string $search with the string $replace.
00070    *
00071    * @param  string  $search
00072    * @param  string  $replace
00073    * @param  boolean $caseSensitivity
00074    * @return TeamSpeak3_Helper_String
00075    */
00076   public function replace($search, $replace, $caseSensitivity = TRUE)
00077   {
00078     if($caseSensitivity)
00079     {
00080       $this->string = str_replace($search, $replace, $this->string);
00081     }
00082     else
00083     {
00084       $this->string = str_ireplace($search, $replace, $this->string);
00085     }
00086 
00087     return $this;
00088   }
00089 
00090   /**
00091    * This function replaces indexed or associative signs with given values.
00092    *
00093    * @param  array  $args
00094    * @param  string $char
00095    * @return TeamSpeak3_Helper_String
00096    */
00097   public function arg(array $args, $char = "%")
00098   {
00099     $args = array_reverse($args, TRUE);
00100 
00101     foreach($args as $key => $val)
00102     {
00103       $args[$char . $key] = $val;
00104       unset($args[$key]);
00105     }
00106 
00107     $this->string = strtr($this->string, $args);
00108 
00109     return $this;
00110   }
00111 
00112   /**
00113    * Returns true if the string starts with $pattern.
00114    *
00115    * @param  string  $pattern
00116    * @return boolean
00117    */
00118   public function startsWith($pattern)
00119   {
00120     return (substr($this->string, 0, strlen($pattern)) == $pattern) ? TRUE : FALSE;
00121   }
00122 
00123   /**
00124    * Returns true if the string ends with $pattern.
00125    *
00126    * @param  string  $pattern
00127    * @return boolean
00128    */
00129   public function endsWith($pattern)
00130   {
00131     return (substr($this->string, strlen($pattern)*-1) == $pattern) ? TRUE : FALSE;
00132   }
00133 
00134   /**
00135    * Returns the position of the first occurrence of a char in a string.
00136    *
00137    * @param  string $needle
00138    * @return integer
00139    */
00140   public function findFirst($needle)
00141   {
00142     return strpos($this->string, $needle);
00143   }
00144 
00145   /**
00146    * Returns the position of the last occurrence of a char in a string.
00147    *
00148    * @param  string $needle
00149    * @return integer
00150    */
00151   public function findLast($needle)
00152   {
00153     return strrpos($this->string, $needle);
00154   }
00155 
00156   /**
00157    * Returns the lowercased string.
00158    *
00159    * @return TeamSpeak3_Helper_String
00160    */
00161   public function toLower()
00162   {
00163     return new self(strtolower($this->string));
00164   }
00165 
00166   /**
00167    * Returns the uppercased string.
00168    *
00169    * @return TeamSpeak3_Helper_String
00170    */
00171   public function toUpper()
00172   {
00173     return new self(strtoupper($this->string));
00174   }
00175 
00176   /**
00177    * Returns true if the string contains $pattern.
00178    *
00179    * @param  string  $pattern
00180    * @param  booean  $regexp
00181    * @return boolean
00182    */
00183   public function contains($pattern, $regexp = FALSE)
00184   {
00185     if(empty($pattern))
00186     {
00187       return TRUE;
00188     }
00189 
00190     if($regexp)
00191     {
00192       return (preg_match("/" . $pattern . "/i", $this->string)) ? TRUE : FALSE;
00193     }
00194     else
00195     {
00196       return (stristr($this->string, $pattern) !== FALSE) ? TRUE : FALSE;
00197     }
00198   }
00199 
00200   /**
00201    * Returns part of a string.
00202    *
00203    * @param  integer $start
00204    * @param  integer $length
00205    * @return TeamSpeak3_Helper_String
00206    */
00207   public function substr($start, $length = null)
00208   {
00209     $string = ($length !== null) ? substr($this->string, $start, $length) : substr($this->string, $start);
00210 
00211     return new self($string);
00212   }
00213 
00214   /**
00215    * Splits the string into substrings wherever $separator occurs.
00216    *
00217    * @param  string  $separator
00218    * @param  integer $limit
00219    * @return array
00220    */
00221   public function split($separator, $limit = 0)
00222   {
00223     $parts = explode($separator, $this->string, ($limit) ? intval($limit) : $this->count());
00224 
00225     foreach($parts as $key => $val)
00226     {
00227       $parts[$key] = new self($val);
00228     }
00229 
00230     return $parts;
00231   }
00232 
00233   /**
00234    * Appends $part to the string.
00235    *
00236    * @param  string $part
00237    * @return TeamSpeak3_Helper_String
00238    */
00239   public function append($part)
00240   {
00241     $this->string = $this->string . strval($part);
00242 
00243     return $this;
00244   }
00245 
00246   /**
00247    * Prepends $part to the string.
00248    *
00249    * @param  string $part
00250    * @return TeamSpeak3_Helper_String
00251    */
00252   public function prepend($part)
00253   {
00254     $this->string = strval($part) . $this->string;
00255 
00256     return $this;
00257   }
00258 
00259   /**
00260    * Returns a section of the string.
00261    *
00262    * @param  string  $separator
00263    * @param  integer $first
00264    * @param  integer $last
00265    * @return TeamSpeak3_Helper_String
00266    */
00267   public function section($separator, $first = 0, $last = 0)
00268   {
00269     $sections = explode($separator, $this->string);
00270 
00271     $total = count($sections);
00272     $first = intval($first);
00273     $last = intval($last);
00274 
00275     if($first > $total) return null;
00276     if($first > $last) $last = $first;
00277 
00278     for($i = 0; $i < $total; $i++)
00279     {
00280       if($i < $first || $i > $last)
00281       {
00282         unset($sections[$i]);
00283       }
00284     }
00285 
00286     $string = implode($separator, $sections);
00287 
00288     return new self($string);
00289   }
00290 
00291   /**
00292    * Sets the size of the string to $size characters.
00293    *
00294    * @param  integer $size
00295    * @param  string  $char
00296    * @return TeamSpeak3_Helper_String
00297    */
00298   public function resize($size, $char = "\0")
00299   {
00300     $chars = ($size - $this->count());
00301 
00302     if($chars < 0)
00303     {
00304       $this->string = substr($this->string, 0, $chars);
00305     }
00306     elseif($chars > 0)
00307     {
00308       $this->string = str_pad($this->string, $size, strval($char));
00309     }
00310 
00311     return $this;
00312   }
00313 
00314   /**
00315    * Strips whitespaces (or other characters) from the beginning and end of the string.
00316    *
00317    * @return TeamSpeak3_Helper_String
00318    */
00319   public function trim()
00320   {
00321     $this->string = trim($this->string);
00322 
00323     return $this;
00324   }
00325 
00326   /**
00327    * Escapes a string using the TeamSpeak 3 escape patterns.
00328    *
00329    * @return TeamSpeak3_Helper_String
00330    */
00331   public function escape()
00332   {
00333     foreach(TeamSpeak3::getEscapePatterns() as $search => $replace)
00334     {
00335       $this->string = str_replace($search, $replace, $this->string);
00336     }
00337 
00338     return $this;
00339   }
00340 
00341   /**
00342    * Unescapes a string using the TeamSpeak 3 escape patterns.
00343    *
00344    * @return TeamSpeak3_Helper_String
00345    */
00346   public function unescape()
00347   {
00348     $this->string = strtr($this->string, array_flip(TeamSpeak3::getEscapePatterns()));
00349 
00350     return $this;
00351   }
00352 
00353   /**
00354    * Removes any non alphanumeric characters from the string.
00355    *
00356    * @return TeamSpeak3_Helper_String
00357    */
00358   public function filterAlnum()
00359   {
00360     $this->string = preg_replace("/[^[:alnum:]]/", "",  $this->string);
00361 
00362     return $this;
00363   }
00364 
00365   /**
00366    * Removes any non alphabetic characters from the string.
00367    *
00368    * @return TeamSpeak3_Helper_String
00369    */
00370   public function filterAlpha()
00371   {
00372     $this->string = preg_replace("/[^[:alpha:]]/", "",  $this->string);
00373 
00374     return $this;
00375   }
00376 
00377   /**
00378    * Removes any non numeric characters from the string.
00379    *
00380    * @return TeamSpeak3_Helper_String
00381    */
00382   public function filterDigits()
00383   {
00384     $this->string = preg_replace("/[^[:digit:]]/", "",  $this->string);
00385 
00386     return $this;
00387   }
00388 
00389   /**
00390    * Returns TRUE if the string is a numeric value.
00391    *
00392    * @return boolean
00393    */
00394   public function isInt()
00395   {
00396     return (is_numeric($this->string) && !$this->contains(".")) ? TRUE : FALSE;
00397   }
00398 
00399   /**
00400    * Returns the integer value of the string.
00401    *
00402    * @return float
00403    * @return integer
00404    */
00405   public function toInt()
00406   {
00407     if($this->string == pow(2, 63) || $this->string == pow(2, 64))
00408     {
00409       return -1;
00410     }
00411 
00412     return ($this->string > pow(2, 31)) ? floatval($this->string) : intval($this->string);
00413   }
00414 
00415   /**
00416    * Calculates and returns the crc32 polynomial of the string.
00417    *
00418    * @return string
00419    */
00420   public function toCrc32()
00421   {
00422     return crc32($this->string);
00423   }
00424 
00425   /**
00426    * Calculates and returns the md5 checksum of the string.
00427    *
00428    * @return string
00429    */
00430   public function toMd5()
00431   {
00432     return md5($this->string);
00433   }
00434 
00435   /**
00436    * Calculates and returns the sha1 checksum of the string.
00437    *
00438    * @return string
00439    */
00440   public function toSha1()
00441   {
00442     return sha1($this->string);
00443   }
00444 
00445   /**
00446    * Returns TRUE if the string is UTF-8 encoded. This method searches for non-ascii multibyte
00447    * sequences in the UTF-8 range.
00448    *
00449    * @return boolean
00450    */
00451   public function isUtf8()
00452   {
00453     //return (utf8_encode(utf8_decode($this->string)) == $this->string) ? TRUE : FALSE;
00454 
00455     $pattern = array();
00456 
00457     $pattern[] = "[\xC2-\xDF][\x80-\xBF]";            // non-overlong 2-byte
00458     $pattern[] = "\xE0[\xA0-\xBF][\x80-\xBF]";        // excluding overlongs
00459     $pattern[] = "[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}"; // straight 3-byte
00460     $pattern[] = "\xED[\x80-\x9F][\x80-\xBF]";        // excluding surrogates
00461     $pattern[] = "\xF0[\x90-\xBF][\x80-\xBF]{2}";     // planes 1-3
00462     $pattern[] = "[\xF1-\xF3][\x80-\xBF]{3}";         // planes 4-15
00463     $pattern[] = "\xF4[\x80-\x8F][\x80-\xBF]{2}";     // plane 16
00464 
00465     return preg_match("%(?:" . implode("|", $pattern) . ")+%xs", $this->string);;
00466   }
00467 
00468   /**
00469    * Converts the string to UTF-8.
00470    *
00471    * @return TeamSpeak3_Helper_String
00472    */
00473   public function toUtf8()
00474   {
00475     if(!$this->isUtf8())
00476     {
00477       $this->string = utf8_encode($this->string);
00478     }
00479 
00480     return $this;
00481   }
00482 
00483   /**
00484    * Encodes the string with MIME base64 and returns the result.
00485    *
00486    * @return string
00487    */
00488   public function toBase64()
00489   {
00490     return base64_encode($this->string);
00491   }
00492 
00493   /**
00494    * Decodes the string with MIME base64 and returns the result as an TeamSpeak3_Helper_String
00495    *
00496    * @param  string
00497    * @return TeamSpeak3_Helper_String
00498    */
00499   public static function fromBase64($base64)
00500   {
00501     return new self(base64_decode($base64));
00502   }
00503 
00504   /**
00505    * Returns the hexadecimal value of the string.
00506    *
00507    * @return string
00508    */
00509   public function toHex()
00510   {
00511     $hex = "";
00512 
00513     foreach($this as $char)
00514     {
00515       $hex .= $char->toHex();
00516     }
00517 
00518     return $hex;
00519   }
00520 
00521   /**
00522    * Returns the TeamSpeak3_Helper_String based on a given hex value.
00523    *
00524    * @param  string
00525    * @throws TeamSpeak3_Helper_Exception
00526    * @return TeamSpeak3_Helper_String
00527    */
00528   public static function fromHex($hex)
00529   {
00530     $string = "";
00531 
00532     if(strlen($hex)%2 == 1)
00533     {
00534       throw new TeamSpeak3_Helper_Exception("given parameter '" . $hex . "' is not a valid hexadecimal number");
00535     }
00536 
00537     foreach(str_split($hex, 2) as $chunk)
00538     {
00539       $string .= chr(hexdec($chunk));
00540     }
00541 
00542     return new self($string);
00543   }
00544 
00545   /**
00546    * Replaces space characters with percent encoded strings.
00547    *
00548    * @return string
00549    */
00550   public function spaceToPercent()
00551   {
00552     return str_replace(" ", "%20", $this->string);
00553   }
00554 
00555   /**
00556    * Returns the string as a standard string
00557    *
00558    * @return string
00559    */
00560   public function toString()
00561   {
00562     return $this->string;
00563   }
00564 
00565   /**
00566    * Magical function that allows you to call PHP's built-in string functions on the TeamSpeak3_Helper_String object.
00567    *
00568    * @param  string $function
00569    * @param  array  $args
00570    * @throws TeamSpeak3_Helper_Exception
00571    * @return TeamSpeak3_Helper_String
00572    */
00573   public function __call($function, $args)
00574   {
00575     if(!function_exists($function))
00576     {
00577       throw new TeamSpeak3_Helper_Exception("cannot call undefined function '" . $function . "' on this object");
00578     }
00579 
00580     if(count($args))
00581     {
00582       if(($key = array_search($this, $args, TRUE)) !== FALSE)
00583       {
00584         $args[$key] = $this->string;
00585       }
00586       else
00587       {
00588         throw new TeamSpeak3_Helper_Exception("cannot call undefined function '" . $function . "' without the " . __CLASS__ . " object parameter");
00589       }
00590 
00591       $return = call_user_func_array($function, $args);
00592     }
00593     else
00594     {
00595       $return = call_user_func($function, $this->string);
00596     }
00597 
00598     if(is_string($return))
00599     {
00600       $this->string = $return;
00601     }
00602     else
00603     {
00604       return $return;
00605     }
00606 
00607     return $this;
00608   }
00609 
00610   /**
00611    * Returns the character as a standard string.
00612    *
00613    * @return string
00614    */
00615   public function __toString()
00616   {
00617     return (string) $this->string;
00618   }
00619 
00620   /**
00621    * @ignore
00622    */
00623   public function count()
00624   {
00625     return strlen($this->string);
00626   }
00627 
00628   /**
00629    * @ignore
00630    */
00631   public function rewind()
00632   {
00633     $this->position = 0;
00634   }
00635 
00636   /**
00637    * @ignore
00638    */
00639   public function valid()
00640   {
00641     return $this->position < $this->count();
00642   }
00643 
00644   /**
00645    * @ignore
00646    */
00647   public function key()
00648   {
00649     return $this->position;
00650   }
00651 
00652   /**
00653    * @ignore
00654    */
00655   public function current()
00656   {
00657     return new TeamSpeak3_Helper_Char($this->string{$this->position});
00658   }
00659 
00660   /**
00661    * @ignore
00662    */
00663   public function next()
00664   {
00665     $this->position++;
00666   }
00667 
00668   /**
00669    * @ignore
00670    */
00671   public function offsetExists($offset)
00672   {
00673     return ($offset < strlen($this->string)) ? TRUE : FALSE;
00674   }
00675 
00676   /**
00677    * @ignore
00678    */
00679   public function offsetGet($offset)
00680   {
00681     return ($this->offsetExists($offset)) ? new TeamSpeak3_Helper_Char($this->string{$offset}) : null;
00682   }
00683 
00684   /**
00685    * @ignore
00686    */
00687   public function offsetSet($offset, $value)
00688   {
00689     if(!$this->offsetExists($offset)) return;
00690 
00691     $this->string{$offset} = strval($value);
00692   }
00693 
00694   /**
00695    * @ignore
00696    */
00697   public function offsetUnset($offset)
00698   {
00699     if(!$this->offsetExists($offset)) return;
00700 
00701     $this->string = substr_replace($this->string, "", $offset, 1);
00702   }
00703 }
 All Classes Files Functions Variables