String.php 19 KB

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