TeamSpeak 3 PHP Framework  1.1.16
Copyright © Planet TeamSpeak. All rights reserved.
 All Classes Namespaces Files Functions Variables Pages
String.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @file
5  * TeamSpeak 3 PHP Framework
6  *
7  * $Id: String.php 8/31/2012 11:06:09 scp@orilla $
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @package TeamSpeak3
23  * @version 1.1.16
24  * @author Sven 'ScP' Paulsen
25  * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
26  */
27 
28 /**
29  * @class TeamSpeak3_Helper_String
30  * @brief Helper class for string handling.
31  */
32 class TeamSpeak3_Helper_String implements ArrayAccess, Iterator, Countable
33 {
34  /**
35  * Stores the original string.
36  *
37  * @var string
38  */
39  protected $string;
40 
41  /**
42  * @ignore
43  */
44  protected $position = 0;
45 
46  /**
47  * The TeamSpeak3_Helper_String constructor.
48  *
49  * @param string $string
50  * @return TeamSpeak3_Helper_String
51  */
52  public function __construct($string)
53  {
54  $this->string = strval($string);
55  }
56 
57  /**
58  * Returns a TeamSpeak3_Helper_String object for thegiven string.
59  *
60  * @param string $string
61  * @return TeamSpeak3_Helper_String
62  */
63  public static function factory($string)
64  {
65  return new self($string);
66  }
67 
68  /**
69  * Replaces every occurrence of the string $search with the string $replace.
70  *
71  * @param string $search
72  * @param string $replace
73  * @param boolean $caseSensitivity
74  * @return TeamSpeak3_Helper_String
75  */
76  public function replace($search, $replace, $caseSensitivity = TRUE)
77  {
78  if($caseSensitivity)
79  {
80  $this->string = str_replace($search, $replace, $this->string);
81  }
82  else
83  {
84  $this->string = str_ireplace($search, $replace, $this->string);
85  }
86 
87  return $this;
88  }
89 
90  /**
91  * This function replaces indexed or associative signs with given values.
92  *
93  * @param array $args
94  * @param string $char
95  * @return TeamSpeak3_Helper_String
96  */
97  public function arg(array $args, $char = "%")
98  {
99  $args = array_reverse($args, TRUE);
100 
101  foreach($args as $key => $val)
102  {
103  $args[$char . $key] = $val;
104  unset($args[$key]);
105  }
106 
107  $this->string = strtr($this->string, $args);
108 
109  return $this;
110  }
111 
112  /**
113  * Returns true if the string starts with $pattern.
114  *
115  * @param string $pattern
116  * @return boolean
117  */
118  public function startsWith($pattern)
119  {
120  return (substr($this->string, 0, strlen($pattern)) == $pattern) ? TRUE : FALSE;
121  }
122 
123  /**
124  * Returns true if the string ends with $pattern.
125  *
126  * @param string $pattern
127  * @return boolean
128  */
129  public function endsWith($pattern)
130  {
131  return (substr($this->string, strlen($pattern)*-1) == $pattern) ? TRUE : FALSE;
132  }
133 
134  /**
135  * Returns the position of the first occurrence of a char in a string.
136  *
137  * @param string $needle
138  * @return integer
139  */
140  public function findFirst($needle)
141  {
142  return strpos($this->string, $needle);
143  }
144 
145  /**
146  * Returns the position of the last occurrence of a char in a string.
147  *
148  * @param string $needle
149  * @return integer
150  */
151  public function findLast($needle)
152  {
153  return strrpos($this->string, $needle);
154  }
155 
156  /**
157  * Returns the lowercased string.
158  *
159  * @return TeamSpeak3_Helper_String
160  */
161  public function toLower()
162  {
163  return new self(strtolower($this->string));
164  }
165 
166  /**
167  * Returns the uppercased string.
168  *
169  * @return TeamSpeak3_Helper_String
170  */
171  public function toUpper()
172  {
173  return new self(strtoupper($this->string));
174  }
175 
176  /**
177  * Returns true if the string contains $pattern.
178  *
179  * @param string $pattern
180  * @param booean $regexp
181  * @return boolean
182  */
183  public function contains($pattern, $regexp = FALSE)
184  {
185  if(empty($pattern))
186  {
187  return TRUE;
188  }
189 
190  if($regexp)
191  {
192  return (preg_match("/" . $pattern . "/i", $this->string)) ? TRUE : FALSE;
193  }
194  else
195  {
196  return (stristr($this->string, $pattern) !== FALSE) ? TRUE : FALSE;
197  }
198  }
199 
200  /**
201  * Returns part of a string.
202  *
203  * @param integer $start
204  * @param integer $length
205  * @return TeamSpeak3_Helper_String
206  */
207  public function substr($start, $length = null)
208  {
209  $string = ($length !== null) ? substr($this->string, $start, $length) : substr($this->string, $start);
210 
211  return new self($string);
212  }
213 
214  /**
215  * Splits the string into substrings wherever $separator occurs.
216  *
217  * @param string $separator
218  * @param integer $limit
219  * @return array
220  */
221  public function split($separator, $limit = 0)
222  {
223  $parts = explode($separator, $this->string, ($limit) ? intval($limit) : $this->count());
224 
225  foreach($parts as $key => $val)
226  {
227  $parts[$key] = new self($val);
228  }
229 
230  return $parts;
231  }
232 
233  /**
234  * Appends $part to the string.
235  *
236  * @param string $part
237  * @return TeamSpeak3_Helper_String
238  */
239  public function append($part)
240  {
241  $this->string = $this->string . strval($part);
242 
243  return $this;
244  }
245 
246  /**
247  * Prepends $part to the string.
248  *
249  * @param string $part
250  * @return TeamSpeak3_Helper_String
251  */
252  public function prepend($part)
253  {
254  $this->string = strval($part) . $this->string;
255 
256  return $this;
257  }
258 
259  /**
260  * Returns a section of the string.
261  *
262  * @param string $separator
263  * @param integer $first
264  * @param integer $last
265  * @return TeamSpeak3_Helper_String
266  */
267  public function section($separator, $first = 0, $last = 0)
268  {
269  $sections = explode($separator, $this->string);
270 
271  $total = count($sections);
272  $first = intval($first);
273  $last = intval($last);
274 
275  if($first > $total) return null;
276  if($first > $last) $last = $first;
277 
278  for($i = 0; $i < $total; $i++)
279  {
280  if($i < $first || $i > $last)
281  {
282  unset($sections[$i]);
283  }
284  }
285 
286  $string = implode($separator, $sections);
287 
288  return new self($string);
289  }
290 
291  /**
292  * Sets the size of the string to $size characters.
293  *
294  * @param integer $size
295  * @param string $char
296  * @return TeamSpeak3_Helper_String
297  */
298  public function resize($size, $char = "\0")
299  {
300  $chars = ($size - $this->count());
301 
302  if($chars < 0)
303  {
304  $this->string = substr($this->string, 0, $chars);
305  }
306  elseif($chars > 0)
307  {
308  $this->string = str_pad($this->string, $size, strval($char));
309  }
310 
311  return $this;
312  }
313 
314  /**
315  * Strips whitespaces (or other characters) from the beginning and end of the string.
316  *
317  * @return TeamSpeak3_Helper_String
318  */
319  public function trim()
320  {
321  $this->string = trim($this->string);
322 
323  return $this;
324  }
325 
326  /**
327  * Escapes a string using the TeamSpeak 3 escape patterns.
328  *
329  * @return TeamSpeak3_Helper_String
330  */
331  public function escape()
332  {
333  foreach(TeamSpeak3::getEscapePatterns() as $search => $replace)
334  {
335  $this->string = str_replace($search, $replace, $this->string);
336  }
337 
338  return $this;
339  }
340 
341  /**
342  * Unescapes a string using the TeamSpeak 3 escape patterns.
343  *
344  * @return TeamSpeak3_Helper_String
345  */
346  public function unescape()
347  {
348  $this->string = strtr($this->string, array_flip(TeamSpeak3::getEscapePatterns()));
349 
350  return $this;
351  }
352 
353  /**
354  * Removes any non alphanumeric characters from the string.
355  *
356  * @return TeamSpeak3_Helper_String
357  */
358  public function filterAlnum()
359  {
360  $this->string = preg_replace("/[^[:alnum:]]/", "", $this->string);
361 
362  return $this;
363  }
364 
365  /**
366  * Removes any non alphabetic characters from the string.
367  *
368  * @return TeamSpeak3_Helper_String
369  */
370  public function filterAlpha()
371  {
372  $this->string = preg_replace("/[^[:alpha:]]/", "", $this->string);
373 
374  return $this;
375  }
376 
377  /**
378  * Removes any non numeric characters from the string.
379  *
380  * @return TeamSpeak3_Helper_String
381  */
382  public function filterDigits()
383  {
384  $this->string = preg_replace("/[^[:digit:]]/", "", $this->string);
385 
386  return $this;
387  }
388 
389  /**
390  * Returns TRUE if the string is a numeric value.
391  *
392  * @return boolean
393  */
394  public function isInt()
395  {
396  return (is_numeric($this->string) && !$this->contains(".")) ? TRUE : FALSE;
397  }
398 
399  /**
400  * Returns the integer value of the string.
401  *
402  * @return float
403  * @return integer
404  */
405  public function toInt()
406  {
407  if($this->string == pow(2, 63) || $this->string == pow(2, 64))
408  {
409  return -1;
410  }
411 
412  return ($this->string > pow(2, 31)) ? floatval($this->string) : intval($this->string);
413  }
414 
415  /**
416  * Calculates and returns the crc32 polynomial of the string.
417  *
418  * @return string
419  */
420  public function toCrc32()
421  {
422  return crc32($this->string);
423  }
424 
425  /**
426  * Calculates and returns the md5 checksum of the string.
427  *
428  * @return string
429  */
430  public function toMd5()
431  {
432  return md5($this->string);
433  }
434 
435  /**
436  * Calculates and returns the sha1 checksum of the string.
437  *
438  * @return string
439  */
440  public function toSha1()
441  {
442  return sha1($this->string);
443  }
444 
445  /**
446  * Returns TRUE if the string is UTF-8 encoded. This method searches for non-ascii multibyte
447  * sequences in the UTF-8 range.
448  *
449  * @return boolean
450  */
451  public function isUtf8()
452  {
453  //return (utf8_encode(utf8_decode($this->string)) == $this->string) ? TRUE : FALSE;
454 
455  $pattern = array();
456 
457  $pattern[] = "[\xC2-\xDF][\x80-\xBF]"; // non-overlong 2-byte
458  $pattern[] = "\xE0[\xA0-\xBF][\x80-\xBF]"; // excluding overlongs
459  $pattern[] = "[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}"; // straight 3-byte
460  $pattern[] = "\xED[\x80-\x9F][\x80-\xBF]"; // excluding surrogates
461  $pattern[] = "\xF0[\x90-\xBF][\x80-\xBF]{2}"; // planes 1-3
462  $pattern[] = "[\xF1-\xF3][\x80-\xBF]{3}"; // planes 4-15
463  $pattern[] = "\xF4[\x80-\x8F][\x80-\xBF]{2}"; // plane 16
464 
465  return preg_match("%(?:" . implode("|", $pattern) . ")+%xs", $this->string);
466  }
467 
468  /**
469  * Converts the string to UTF-8.
470  *
471  * @return TeamSpeak3_Helper_String
472  */
473  public function toUtf8()
474  {
475  if(!$this->isUtf8())
476  {
477  $this->string = utf8_encode($this->string);
478  }
479 
480  return $this;
481  }
482 
483  /**
484  * Encodes the string with MIME base64 and returns the result.
485  *
486  * @return string
487  */
488  public function toBase64()
489  {
490  return base64_encode($this->string);
491  }
492 
493  /**
494  * Decodes the string with MIME base64 and returns the result as an TeamSpeak3_Helper_String
495  *
496  * @param string
497  * @return TeamSpeak3_Helper_String
498  */
499  public static function fromBase64($base64)
500  {
501  return new self(base64_decode($base64));
502  }
503 
504  /**
505  * Returns the hexadecimal value of the string.
506  *
507  * @return string
508  */
509  public function toHex()
510  {
511  $hex = "";
512 
513  foreach($this as $char)
514  {
515  $hex .= $char->toHex();
516  }
517 
518  return $hex;
519  }
520 
521  /**
522  * Returns the TeamSpeak3_Helper_String based on a given hex value.
523  *
524  * @param string
525  * @throws TeamSpeak3_Helper_Exception
526  * @return TeamSpeak3_Helper_String
527  */
528  public static function fromHex($hex)
529  {
530  $string = "";
531 
532  if(strlen($hex)%2 == 1)
533  {
534  throw new TeamSpeak3_Helper_Exception("given parameter '" . $hex . "' is not a valid hexadecimal number");
535  }
536 
537  foreach(str_split($hex, 2) as $chunk)
538  {
539  $string .= chr(hexdec($chunk));
540  }
541 
542  return new self($string);
543  }
544 
545  /**
546  * Returns the string transliterated from UTF-8 to Latin.
547  *
548  * @return TeamSpeak3_Helper_String
549  */
550  public function transliterate()
551  {
552  $utf8_accents = array(
553  "à" => "a",
554  "ô" => "o",
555  "Ä?" => "d",
556  "ḟ" => "f",
557  "ë" => "e",
558  "Å¡" => "s",
559  "Æ¡" => "o",
560  "ß" => "ss",
561  "ă" => "a",
562  "Å™" => "r",
563  "È›" => "t",
564  "ň" => "n",
565  "Ä?" => "a",
566  "Ä·" => "k",
567  "Å?" => "s",
568  "ỳ" => "y",
569  "ņ" => "n",
570  "ĺ" => "l",
571  "ħ" => "h",
572  "á¹—" => "p",
573  "ó" => "o",
574  "ú" => "u",
575  "Ä›" => "e",
576  "é" => "e",
577  "ç" => "c",
578  "áº?" => "w",
579  "Ä‹" => "c",
580  "õ" => "o",
581  "ṡ" => "s",
582  "ø" => "o",
583  "Ä£" => "g",
584  "ŧ" => "t",
585  "È™" => "s",
586  "Ä—" => "e",
587  "ĉ" => "c",
588  "Å›" => "s",
589  "î" => "i",
590  "ű" => "u",
591  "ć" => "c",
592  "Ä™" => "e",
593  "ŵ" => "w",
594  "ṫ" => "t",
595  "Å«" => "u",
596  "Ä?" => "c",
597  "ö" => "oe",
598  "è" => "e",
599  "Å·" => "y",
600  "Ä…" => "a",
601  "Å‚" => "l",
602  "ų" => "u",
603  "ů" => "u",
604  "ÅŸ" => "s",
605  "ÄŸ" => "g",
606  "ļ" => "l",
607  "Æ’" => "f",
608  "ž" => "z",
609  "ẃ" => "w",
610  "ḃ" => "b",
611  "Ã¥" => "a",
612  "ì" => "i",
613  "ï" => "i",
614  "ḋ" => "d",
615  "Å¥" => "t",
616  "Å—" => "r",
617  "ä" => "ae",
618  "í" => "i",
619  "Å•" => "r",
620  "ê" => "e",
621  "ü" => "ue",
622  "ò" => "o",
623  "Ä“" => "e",
624  "ñ" => "n",
625  "Å„" => "n",
626  "Ä¥" => "h",
627  "Ä?" => "g",
628  "Ä‘" => "d",
629  "ĵ" => "j",
630  "ÿ" => "y",
631  "Å©" => "u",
632  "Å­" => "u",
633  "ư" => "u",
634  "Å£" => "t",
635  "ý" => "y",
636  "Å‘" => "o",
637  "â" => "a",
638  "ľ" => "l",
639  "ẅ" => "w",
640  "ż" => "z",
641  "Ä«" => "i",
642  "ã" => "a",
643  "Ä¡" => "g",
644  "á¹?" => "m",
645  "Å?" => "o",
646  "Ä©" => "i",
647  "ù" => "u",
648  "į" => "i",
649  "ź" => "z",
650  "á" => "a",
651  "û" => "u",
652  "þ" => "th",
653  "ð" => "dh",
654  "æ" => "ae",
655  "µ" => "u",
656  "Ä•" => "e",
657  "Å“" => "oe",
658  "À" => "A",
659  "Ô" => "O",
660  "ÄŽ" => "D",
661  "Ḟ" => "F",
662  "Ë" => "E",
663  "Å " => "S",
664  "Æ " => "O",
665  "Ä‚" => "A",
666  "Ř" => "R",
667  "Èš" => "T",
668  "Ň" => "N",
669  "Ä€" => "A",
670  "Ķ" => "K",
671  "Åœ" => "S",
672  "Ỳ" => "Y",
673  "Å…" => "N",
674  "Ĺ" => "L",
675  "Ħ" => "H",
676  "á¹–" => "P",
677  "Ó" => "O",
678  "Ú" => "U",
679  "Äš" => "E",
680  "É" => "E",
681  "Ç" => "C",
682  "Ẁ" => "W",
683  "ÄŠ" => "C",
684  "Õ" => "O",
685  "á¹ " => "S",
686  "Ø" => "O",
687  "Ä¢" => "G",
688  "Ŧ" => "T",
689  "Ș" => "S",
690  "Ä–" => "E",
691  "Ĉ" => "C",
692  "Åš" => "S",
693  "ÃŽ" => "I",
694  "Ű" => "U",
695  "Ć" => "C",
696  "Ę" => "E",
697  "Å´" => "W",
698  "Ṫ" => "T",
699  "Ū" => "U",
700  "ÄŒ" => "C",
701  "Ö" => "Oe",
702  "È" => "E",
703  "Ŷ" => "Y",
704  "Ä„" => "A",
705  "Å?" => "L",
706  "Ų" => "U",
707  "Å®" => "U",
708  "Åž" => "S",
709  "Äž" => "G",
710  "Ä»" => "L",
711  "Æ‘" => "F",
712  "Ž" => "Z",
713  "Ẃ" => "W",
714  "Ḃ" => "B",
715  "Ã…" => "A",
716  "ÃŒ" => "I",
717  "Ã?" => "I",
718  "Ḋ" => "D",
719  "Ť" => "T",
720  "Å–" => "R",
721  "Ä" => "Ae",
722  "Ã?" => "I",
723  "Å”" => "R",
724  "Ê" => "E",
725  "Ü" => "Ue",
726  "Ã’" => "O",
727  "Ä’" => "E",
728  "Ñ" => "N",
729  "Ń" => "N",
730  "Ĥ" => "H",
731  "Äœ" => "G",
732  "Ä?" => "D",
733  "Ä´" => "J",
734  "Ÿ" => "Y",
735  "Ũ" => "U",
736  "Ŭ" => "U",
737  "Ư" => "U",
738  "Å¢" => "T",
739  "Ã?" => "Y",
740  "Å?" => "O",
741  "Â" => "A",
742  "Ľ" => "L",
743  "Ẅ" => "W",
744  "Å»" => "Z",
745  "Ī" => "I",
746  "Ã" => "A",
747  "Ä " => "G",
748  "á¹€" => "M",
749  "ÅŒ" => "O",
750  "Ĩ" => "I",
751  "Ù" => "U",
752  "Ä®" => "I",
753  "Ź" => "Z",
754  "Ã?" => "A",
755  "Û" => "U",
756  "Þ" => "Th",
757  "Ã?" => "Dh",
758  "Æ" => "Ae",
759  "Ä”" => "E",
760  "Å’" => "Oe",
761  );
762 
763  return new self($this->toUtf8()->replace(array_keys($utf8_accents), array_values($utf8_accents)));
764  }
765 
766  /**
767  * Processes the string and replaces all accented UTF-8 characters by unaccented ASCII-7 "equivalents",
768  * whitespaces are replaced by a pre-defined spacer and the string is lowercase.
769  *
770  * @param string $spacer
771  * @return TeamSpeak3_Helper_String
772  */
773  public function uriSafe($spacer = "-")
774  {
775  $this->string = str_replace($spacer, " ", $this->string);
776  $this->string = $this->transliterate();
777  $this->string = preg_replace("/(\s|[^A-Za-z0-9\-])+/", $spacer, trim(strtolower($this->string)));
778  $this->string = trim($this->string, $spacer);
779 
780  return new self($this->string);
781  }
782 
783  /**
784  * Replaces space characters with percent encoded strings.
785  *
786  * @return string
787  */
788  public function spaceToPercent()
789  {
790  return str_replace(" ", "%20", $this->string);
791  }
792 
793  /**
794  * Returns the string as a standard string
795  *
796  * @return string
797  */
798  public function toString()
799  {
800  return $this->string;
801  }
802 
803  /**
804  * Magical function that allows you to call PHP's built-in string functions on the TeamSpeak3_Helper_String object.
805  *
806  * @param string $function
807  * @param array $args
808  * @throws TeamSpeak3_Helper_Exception
809  * @return TeamSpeak3_Helper_String
810  */
811  public function __call($function, $args)
812  {
813  if(!function_exists($function))
814  {
815  throw new TeamSpeak3_Helper_Exception("cannot call undefined function '" . $function . "' on this object");
816  }
817 
818  if(count($args))
819  {
820  if(($key = array_search($this, $args, TRUE)) !== FALSE)
821  {
822  $args[$key] = $this->string;
823  }
824  else
825  {
826  throw new TeamSpeak3_Helper_Exception("cannot call undefined function '" . $function . "' without the " . __CLASS__ . " object parameter");
827  }
828 
829  $return = call_user_func_array($function, $args);
830  }
831  else
832  {
833  $return = call_user_func($function, $this->string);
834  }
835 
836  if(is_string($return))
837  {
838  $this->string = $return;
839  }
840  else
841  {
842  return $return;
843  }
844 
845  return $this;
846  }
847 
848  /**
849  * Returns the character as a standard string.
850  *
851  * @return string
852  */
853  public function __toString()
854  {
855  return (string) $this->string;
856  }
857 
858  /**
859  * @ignore
860  */
861  public function count()
862  {
863  return strlen($this->string);
864  }
865 
866  /**
867  * @ignore
868  */
869  public function rewind()
870  {
871  $this->position = 0;
872  }
873 
874  /**
875  * @ignore
876  */
877  public function valid()
878  {
879  return $this->position < $this->count();
880  }
881 
882  /**
883  * @ignore
884  */
885  public function key()
886  {
887  return $this->position;
888  }
889 
890  /**
891  * @ignore
892  */
893  public function current()
894  {
895  return new TeamSpeak3_Helper_Char($this->string{$this->position});
896  }
897 
898  /**
899  * @ignore
900  */
901  public function next()
902  {
903  $this->position++;
904  }
905 
906  /**
907  * @ignore
908  */
909  public function offsetExists($offset)
910  {
911  return ($offset < strlen($this->string)) ? TRUE : FALSE;
912  }
913 
914  /**
915  * @ignore
916  */
917  public function offsetGet($offset)
918  {
919  return ($this->offsetExists($offset)) ? new TeamSpeak3_Helper_Char($this->string{$offset}) : null;
920  }
921 
922  /**
923  * @ignore
924  */
925  public function offsetSet($offset, $value)
926  {
927  if(!$this->offsetExists($offset)) return;
928 
929  $this->string{$offset} = strval($value);
930  }
931 
932  /**
933  * @ignore
934  */
935  public function offsetUnset($offset)
936  {
937  if(!$this->offsetExists($offset)) return;
938 
939  $this->string = substr_replace($this->string, "", $offset, 1);
940  }
941 }