3namespace PlanetTeamSpeak\TeamSpeak3Framework\Helper;
9use PlanetTeamSpeak\TeamSpeak3Framework\Exception\HelperException;
10use PlanetTeamSpeak\TeamSpeak3Framework\TeamSpeak3;
18class StringHelper implements ArrayAccess, Iterator, Countable, JsonSerializable
40 $this->
string = $string;
62 public function replace(array|
string $search, array|
string $replace,
bool $caseSensitivity =
true): static
64 if ($caseSensitivity) {
65 $this->
string = str_replace($search, $replace, $this->
string);
67 $this->
string = str_ireplace($search, $replace, $this->
string);
80 public function arg(array $args,
string $char =
"%"): static
82 $args = array_reverse($args,
true);
84 foreach ($args as $key => $val) {
85 $args[$char . $key] = $val;
89 $this->
string = strtr($this->
string, $args);
102 return str_starts_with($this->
string, $pattern);
113 return str_ends_with($this->
string, $pattern);
124 return strpos($this->
string, $needle);
135 return strrpos($this->
string, $needle);
145 return new self(strtolower($this->
string));
155 return new self(strtoupper($this->
string));
165 public function contains(
string $pattern,
bool $regexp =
false): bool
167 if (empty($pattern)) {
172 return boolval(preg_match(sprintf(
"/%s/i", $pattern), $this->
string));
174 return stristr($this->
string, $pattern) !==
false;
187 $string = ($length !==
null) ?
substr($this->
string, $start, $length) :
substr($this->
string, $start);
199 public function split(
string $separator,
int $limit = 0): array
201 $parts = explode($separator, $this->
string, ($limit) ?: $this->
count());
203 foreach ($parts as $key => $val) {
204 $parts[$key] =
new self($val);
216 public function append(
string $part): static
218 $this->
string = $this->
string . $part;
246 $sections = explode($separator, $this->
string);
248 $total =
count($sections);
250 if ($first > $total) {
253 if ($first > $last) {
257 for ($i = 0; $i < $total; $i++) {
258 if ($i < $first || $i > $last) {
259 unset($sections[$i]);
263 $string = implode($separator, $sections);
275 public function resize(
int $size,
string $char =
"\0"): static
277 $chars = ($size - $this->
count());
280 $this->
string =
substr($this->
string, 0, $chars);
281 } elseif ($chars > 0) {
282 $this->
string = str_pad($this->
string, $size, $char);
295 $this->
string =
trim($this->
string);
308 $this->
string = str_replace($search, $replace, $this->
string);
333 $this->
string = preg_replace(
"/[^[:alnum:]]/",
"", $this->
string);
345 $this->
string = preg_replace(
"/[^[:alpha:]]/",
"", $this->
string);
357 $this->
string = preg_replace(
"/[^[:digit:]]/",
"", $this->
string);
369 return is_numeric($this->
string) &&
384 if ($this->
string == pow(2, 63) || $this->
string == pow(2, 64) || $this->
string > pow(2, 31)) {
388 return intval($this->
string);
398 return crc32($this->
string);
408 return md5($this->
string);
418 return sha1($this->
string);
431 $pattern[] =
"[\xC2-\xDF][\x80-\xBF]";
432 $pattern[] =
"\xE0[\xA0-\xBF][\x80-\xBF]";
433 $pattern[] =
"[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}";
434 $pattern[] =
"\xED[\x80-\x9F][\x80-\xBF]";
435 $pattern[] =
"\xF0[\x90-\xBF][\x80-\xBF]{2}";
436 $pattern[] =
"[\xF1-\xF3][\x80-\xBF]{3}";
437 $pattern[] =
"\xF4[\x80-\x8F][\x80-\xBF]{2}";
439 return (
bool)preg_match(
"%(?:" . implode(
"|", $pattern) .
")+%xs", $this->
string);
450 $this->
string = mb_convert_encoding($this->
string,
'UTF-8', mb_list_encodings());
463 return base64_encode($this->
string);
474 return new self(base64_decode($base64));
486 foreach ($this as $char) {
487 $hex .= $char->toHex();
504 if (strlen($hex) % 2 == 1) {
505 throw new HelperException(
"given parameter '" . $hex .
"' is not a valid hexadecimal number");
508 foreach (str_split($hex, 2) as $chunk) {
509 $string .= chr(hexdec($chunk));
733 return new self($this->
toUtf8()->replace(array_keys($utf8_accents), array_values($utf8_accents)));
745 $this->
string = str_replace($spacer,
" ", $this->
string);
747 $this->
string = preg_replace(
"/(\s|[^A-Za-z0-9\-])+/", $spacer,
trim(strtolower($this->
string)));
748 $this->
string =
trim($this->
string, $spacer);
760 return str_replace(
" ",
"%20", $this->
string);
781 public function __call(
string $function, array $args)
783 if (!function_exists($function)) {
784 throw new HelperException(
"cannot call undefined function '" . $function .
"' on this object");
788 if (($key = array_search($this, $args,
true)) !==
false) {
791 throw new HelperException(
"cannot call undefined function '" . $function .
"' without the " . __CLASS__ .
" object parameter");
794 $return = call_user_func_array($function, $args);
796 $return = call_user_func($function, $this->
string);
799 if (is_string($return)) {
800 $this->
string = $return;
825 return $this->
toUtf8()->string;
833 return strlen($this->
string);
849 return $this->position < $this->
count();
855 public function key(): int
866 return new Char($this->
string[$this->position]);
882 return $offset < strlen($this->
string);
891 return ($this->
offsetExists($offset)) ?
new Char($this->
string[$offset]) :
null;
903 $this->
string[$offset] = strval($value);
915 $this->
string = substr_replace($this->
string,
"", $offset, 1);
Enhanced exception class for PlanetTeamSpeak\TeamSpeak3Framework\Helper* objects.
Helper class for char handling.
Helper class for string handling.
static fromHex(string $hex)
findFirst(string $needle)
resize(int $size, string $char="\0")
static fromBase64(string $base64)
section(string $separator, int $first=0, int $last=0)
static factory(string $string)
split(string $separator, int $limit=0)
uriSafe(string $spacer="-")
arg(array $args, string $char="%")
replace(array|string $search, array|string $replace, bool $caseSensitivity=true)
substr(int $start, int $length=null)
offsetSet($offset, $value)
__construct(string $string)
endsWith(string $pattern)
startsWith(string $pattern)
__call(string $function, array $args)
contains(string $pattern, bool $regexp=false)
static getEscapePatterns()