TeamSpeak 3 PHP Framework  1.1.23
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 10/11/2013 11:35:21 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.23
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  $pattern = array();
454 
455  $pattern[] = "[\xC2-\xDF][\x80-\xBF]"; // non-overlong 2-byte
456  $pattern[] = "\xE0[\xA0-\xBF][\x80-\xBF]"; // excluding overlongs
457  $pattern[] = "[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}"; // straight 3-byte
458  $pattern[] = "\xED[\x80-\x9F][\x80-\xBF]"; // excluding surrogates
459  $pattern[] = "\xF0[\x90-\xBF][\x80-\xBF]{2}"; // planes 1-3
460  $pattern[] = "[\xF1-\xF3][\x80-\xBF]{3}"; // planes 4-15
461  $pattern[] = "\xF4[\x80-\x8F][\x80-\xBF]{2}"; // plane 16
462 
463  return preg_match("%(?:" . implode("|", $pattern) . ")+%xs", $this->string);
464  }
465 
466  /**
467  * Converts the string to UTF-8.
468  *
469  * @return TeamSpeak3_Helper_String
470  */
471  public function toUtf8()
472  {
473  if(!$this->isUtf8())
474  {
475  $this->string = utf8_encode($this->string);
476  }
477 
478  return $this;
479  }
480 
481  /**
482  * Encodes the string with MIME base64 and returns the result.
483  *
484  * @return string
485  */
486  public function toBase64()
487  {
488  return base64_encode($this->string);
489  }
490 
491  /**
492  * Decodes the string with MIME base64 and returns the result as an TeamSpeak3_Helper_String
493  *
494  * @param string
495  * @return TeamSpeak3_Helper_String
496  */
497  public static function fromBase64($base64)
498  {
499  return new self(base64_decode($base64));
500  }
501 
502  /**
503  * Returns the hexadecimal value of the string.
504  *
505  * @return string
506  */
507  public function toHex()
508  {
509  $hex = "";
510 
511  foreach($this as $char)
512  {
513  $hex .= $char->toHex();
514  }
515 
516  return $hex;
517  }
518 
519  /**
520  * Returns the TeamSpeak3_Helper_String based on a given hex value.
521  *
522  * @param string
523  * @throws TeamSpeak3_Helper_Exception
524  * @return TeamSpeak3_Helper_String
525  */
526  public static function fromHex($hex)
527  {
528  $string = "";
529 
530  if(strlen($hex)%2 == 1)
531  {
532  throw new TeamSpeak3_Helper_Exception("given parameter '" . $hex . "' is not a valid hexadecimal number");
533  }
534 
535  foreach(str_split($hex, 2) as $chunk)
536  {
537  $string .= chr(hexdec($chunk));
538  }
539 
540  return new self($string);
541  }
542 
543  /**
544  * Returns the string transliterated from UTF-8 to Latin.
545  *
546  * @return TeamSpeak3_Helper_String
547  */
548  public function transliterate()
549  {
550  $utf8_accents = array(
551  "à" => "a",
552  "ô" => "o",
553  "Ä?" => "d",
554  "ḟ" => "f",
555  "ë" => "e",
556  "Å¡" => "s",
557  "Æ¡" => "o",
558  "ß" => "ss",
559  "ă" => "a",
560  "Å™" => "r",
561  "È›" => "t",
562  "ň" => "n",
563  "Ä?" => "a",
564  "Ä·" => "k",
565  "Å?" => "s",
566  "ỳ" => "y",
567  "ņ" => "n",
568  "ĺ" => "l",
569  "ħ" => "h",
570  "á¹—" => "p",
571  "ó" => "o",
572  "ú" => "u",
573  "Ä›" => "e",
574  "é" => "e",
575  "ç" => "c",
576  "áº?" => "w",
577  "Ä‹" => "c",
578  "õ" => "o",
579  "ṡ" => "s",
580  "ø" => "o",
581  "Ä£" => "g",
582  "ŧ" => "t",
583  "È™" => "s",
584  "Ä—" => "e",
585  "ĉ" => "c",
586  "Å›" => "s",
587  "î" => "i",
588  "ű" => "u",
589  "ć" => "c",
590  "Ä™" => "e",
591  "ŵ" => "w",
592  "ṫ" => "t",
593  "Å«" => "u",
594  "Ä?" => "c",
595  "ö" => "oe",
596  "è" => "e",
597  "Å·" => "y",
598  "Ä…" => "a",
599  "Å‚" => "l",
600  "ų" => "u",
601  "ů" => "u",
602  "ÅŸ" => "s",
603  "ÄŸ" => "g",
604  "ļ" => "l",
605  "Æ’" => "f",
606  "ž" => "z",
607  "ẃ" => "w",
608  "ḃ" => "b",
609  "Ã¥" => "a",
610  "ì" => "i",
611  "ï" => "i",
612  "ḋ" => "d",
613  "Å¥" => "t",
614  "Å—" => "r",
615  "ä" => "ae",
616  "í" => "i",
617  "Å•" => "r",
618  "ê" => "e",
619  "ü" => "ue",
620  "ò" => "o",
621  "Ä“" => "e",
622  "ñ" => "n",
623  "Å„" => "n",
624  "Ä¥" => "h",
625  "Ä?" => "g",
626  "Ä‘" => "d",
627  "ĵ" => "j",
628  "ÿ" => "y",
629  "Å©" => "u",
630  "Å­" => "u",
631  "ư" => "u",
632  "Å£" => "t",
633  "ý" => "y",
634  "Å‘" => "o",
635  "â" => "a",
636  "ľ" => "l",
637  "ẅ" => "w",
638  "ż" => "z",
639  "Ä«" => "i",
640  "ã" => "a",
641  "Ä¡" => "g",
642  "á¹?" => "m",
643  "Å?" => "o",
644  "Ä©" => "i",
645  "ù" => "u",
646  "į" => "i",
647  "ź" => "z",
648  "á" => "a",
649  "û" => "u",
650  "þ" => "th",
651  "ð" => "dh",
652  "æ" => "ae",
653  "µ" => "u",
654  "Ä•" => "e",
655  "Å“" => "oe",
656  "À" => "A",
657  "Ô" => "O",
658  "ÄŽ" => "D",
659  "Ḟ" => "F",
660  "Ë" => "E",
661  "Å " => "S",
662  "Æ " => "O",
663  "Ä‚" => "A",
664  "Ř" => "R",
665  "Èš" => "T",
666  "Ň" => "N",
667  "Ä€" => "A",
668  "Ķ" => "K",
669  "Åœ" => "S",
670  "Ỳ" => "Y",
671  "Å…" => "N",
672  "Ĺ" => "L",
673  "Ħ" => "H",
674  "á¹–" => "P",
675  "Ó" => "O",
676  "Ú" => "U",
677  "Äš" => "E",
678  "É" => "E",
679  "Ç" => "C",
680  "Ẁ" => "W",
681  "ÄŠ" => "C",
682  "Õ" => "O",
683  "á¹ " => "S",
684  "Ø" => "O",
685  "Ä¢" => "G",
686  "Ŧ" => "T",
687  "Ș" => "S",
688  "Ä–" => "E",
689  "Ĉ" => "C",
690  "Åš" => "S",
691  "ÃŽ" => "I",
692  "Ű" => "U",
693  "Ć" => "C",
694  "Ę" => "E",
695  "Å´" => "W",
696  "Ṫ" => "T",
697  "Ū" => "U",
698  "ÄŒ" => "C",
699  "Ö" => "Oe",
700  "È" => "E",
701  "Ŷ" => "Y",
702  "Ä„" => "A",
703  "Å?" => "L",
704  "Ų" => "U",
705  "Å®" => "U",
706  "Åž" => "S",
707  "Äž" => "G",
708  "Ä»" => "L",
709  "Æ‘" => "F",
710  "Ž" => "Z",
711  "Ẃ" => "W",
712  "Ḃ" => "B",
713  "Ã…" => "A",
714  "ÃŒ" => "I",
715  "Ã?" => "I",
716  "Ḋ" => "D",
717  "Ť" => "T",
718  "Å–" => "R",
719  "Ä" => "Ae",
720  "Ã?" => "I",
721  "Å”" => "R",
722  "Ê" => "E",
723  "Ü" => "Ue",
724  "Ã’" => "O",
725  "Ä’" => "E",
726  "Ñ" => "N",
727  "Ń" => "N",
728  "Ĥ" => "H",
729  "Äœ" => "G",
730  "Ä?" => "D",
731  "Ä´" => "J",
732  "Ÿ" => "Y",
733  "Ũ" => "U",
734  "Ŭ" => "U",
735  "Ư" => "U",
736  "Å¢" => "T",
737  "Ã?" => "Y",
738  "Å?" => "O",
739  "Â" => "A",
740  "Ľ" => "L",
741  "Ẅ" => "W",
742  "Å»" => "Z",
743  "Ī" => "I",
744  "Ã" => "A",
745  "Ä " => "G",
746  "á¹€" => "M",
747  "ÅŒ" => "O",
748  "Ĩ" => "I",
749  "Ù" => "U",
750  "Ä®" => "I",
751  "Ź" => "Z",
752  "Ã?" => "A",
753  "Û" => "U",
754  "Þ" => "Th",
755  "Ã?" => "Dh",
756  "Æ" => "Ae",
757  "Ä”" => "E",
758  "Å’" => "Oe",
759  );
760 
761  return new self($this->toUtf8()->replace(array_keys($utf8_accents), array_values($utf8_accents)));
762  }
763 
764  /**
765  * Processes the string and replaces all accented UTF-8 characters by unaccented ASCII-7 "equivalents",
766  * whitespaces are replaced by a pre-defined spacer and the string is lowercase.
767  *
768  * @param string $spacer
769  * @return TeamSpeak3_Helper_String
770  */
771  public function uriSafe($spacer = "-")
772  {
773  $this->string = str_replace($spacer, " ", $this->string);
774  $this->string = $this->transliterate();
775  $this->string = preg_replace("/(\s|[^A-Za-z0-9\-])+/", $spacer, trim(strtolower($this->string)));
776  $this->string = trim($this->string, $spacer);
777 
778  return new self($this->string);
779  }
780 
781  /**
782  * Replaces space characters with percent encoded strings.
783  *
784  * @return string
785  */
786  public function spaceToPercent()
787  {
788  return str_replace(" ", "%20", $this->string);
789  }
790 
791  /**
792  * Returns the string as a standard string
793  *
794  * @return string
795  */
796  public function toString()
797  {
798  return $this->string;
799  }
800 
801  /**
802  * Magical function that allows you to call PHP's built-in string functions on the TeamSpeak3_Helper_String object.
803  *
804  * @param string $function
805  * @param array $args
806  * @throws TeamSpeak3_Helper_Exception
807  * @return TeamSpeak3_Helper_String
808  */
809  public function __call($function, $args)
810  {
811  if(!function_exists($function))
812  {
813  throw new TeamSpeak3_Helper_Exception("cannot call undefined function '" . $function . "' on this object");
814  }
815 
816  if(count($args))
817  {
818  if(($key = array_search($this, $args, TRUE)) !== FALSE)
819  {
820  $args[$key] = $this->string;
821  }
822  else
823  {
824  throw new TeamSpeak3_Helper_Exception("cannot call undefined function '" . $function . "' without the " . __CLASS__ . " object parameter");
825  }
826 
827  $return = call_user_func_array($function, $args);
828  }
829  else
830  {
831  $return = call_user_func($function, $this->string);
832  }
833 
834  if(is_string($return))
835  {
836  $this->string = $return;
837  }
838  else
839  {
840  return $return;
841  }
842 
843  return $this;
844  }
845 
846  /**
847  * Returns the character as a standard string.
848  *
849  * @return string
850  */
851  public function __toString()
852  {
853  return (string) $this->string;
854  }
855 
856  /**
857  * @ignore
858  */
859  public function count()
860  {
861  return strlen($this->string);
862  }
863 
864  /**
865  * @ignore
866  */
867  public function rewind()
868  {
869  $this->position = 0;
870  }
871 
872  /**
873  * @ignore
874  */
875  public function valid()
876  {
877  return $this->position < $this->count();
878  }
879 
880  /**
881  * @ignore
882  */
883  public function key()
884  {
885  return $this->position;
886  }
887 
888  /**
889  * @ignore
890  */
891  public function current()
892  {
893  return new TeamSpeak3_Helper_Char($this->string{$this->position});
894  }
895 
896  /**
897  * @ignore
898  */
899  public function next()
900  {
901  $this->position++;
902  }
903 
904  /**
905  * @ignore
906  */
907  public function offsetExists($offset)
908  {
909  return ($offset < strlen($this->string)) ? TRUE : FALSE;
910  }
911 
912  /**
913  * @ignore
914  */
915  public function offsetGet($offset)
916  {
917  return ($this->offsetExists($offset)) ? new TeamSpeak3_Helper_Char($this->string{$offset}) : null;
918  }
919 
920  /**
921  * @ignore
922  */
923  public function offsetSet($offset, $value)
924  {
925  if(!$this->offsetExists($offset)) return;
926 
927  $this->string{$offset} = strval($value);
928  }
929 
930  /**
931  * @ignore
932  */
933  public function offsetUnset($offset)
934  {
935  if(!$this->offsetExists($offset)) return;
936 
937  $this->string = substr_replace($this->string, "", $offset, 1);
938  }
939 }