‪TYPO3CMS  ‪main
StringUtility.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 {
30  public static function ‪cast(mixed $value, ?string $default = null): ?string
31  {
32  if (is_string($value)) {
33  return $value;
34  }
35 
36  if (is_bool($value) || (is_numeric($value) && is_finite($value))) {
37  return (string)$value;
38  }
39 
40  return $default;
41  }
42 
48  public static function ‪filter(mixed $value, ?string $default = null): ?string
49  {
50  return is_string($value) ? $value : $default;
51  }
52 
57  public static function ‪getUniqueId(string $prefix = ''): string
58  {
59  $uniqueId = uniqid($prefix, true);
60  return str_replace('.', '', $uniqueId);
61  }
62 
69  public static function ‪escapeCssSelector(string $selector): string
70  {
71  return preg_replace('/([#:.\\[\\],=@])/', '\\\\$1', $selector);
72  }
73 
79  public static function ‪removeByteOrderMark(string $input): string
80  {
81  if (str_starts_with($input, "\xef\xbb\xbf")) {
82  $input = substr($input, 3);
83  }
84 
85  return $input;
86  }
87 
95  public static function ‪searchStringWildcard(string $haystack, string $needle): bool
96  {
97  $result = false;
98  if ($haystack === $needle) {
99  $result = true;
100  } elseif ($needle) {
101  if (preg_match('/^\\/.+\\/$/', $needle)) {
102  // Regular expression, only "//" is allowed as delimiter
103  $regex = $needle;
104  } else {
105  $needle = str_replace(['*', '?'], ['%%%MANY%%%', '%%%ONE%%%'], $needle);
106  $regex = '/^' . preg_quote($needle, '/') . '$/';
107  // Replace the marker with .* to match anything (wildcard)
108  $regex = str_replace(['%%%MANY%%%', '%%%ONE%%%'], ['.*', '.'], $regex);
109  }
110  $result = (bool)preg_match($regex, $haystack);
111  }
112  return $result;
113  }
114 
122  public static function ‪uniqueList(string $list): string
123  {
124  return implode(',', array_unique(‪GeneralUtility::trimExplode(',', $list, true)));
125  }
126 
131  public static function ‪multibyteStringPad(string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, string $encoding = 'UTF-8'): string
132  {
133  $len = mb_strlen($string, $encoding);
134  $pad_string_len = mb_strlen($pad_string, $encoding);
135  if ($len >= $length || $pad_string_len === 0) {
136  return $string;
137  }
138 
139  switch ($pad_type) {
140  case STR_PAD_RIGHT:
141  $string .= str_repeat($pad_string, (int)(($length - $len) / $pad_string_len));
142  $string .= mb_substr($pad_string, 0, ($length - $len) % $pad_string_len);
143  return $string;
144 
145  case STR_PAD_LEFT:
146  $leftPad = str_repeat($pad_string, (int)(($length - $len) / $pad_string_len));
147  $leftPad .= mb_substr($pad_string, 0, ($length - $len) % $pad_string_len);
148  return $leftPad . $string;
149 
150  case STR_PAD_BOTH:
151  $leftPadCount = (int)(($length - $len) / 2);
152  $len += $leftPadCount;
153  $padded = ((int)($leftPadCount / $pad_string_len)) * $pad_string_len;
154  $leftPad = str_repeat($pad_string, (int)($leftPadCount / $pad_string_len));
155  $leftPad .= mb_substr($pad_string, 0, $leftPadCount - $padded);
156  $string = $leftPad . $string . str_repeat($pad_string, (int)(($length - $len) / $pad_string_len));
157  $string .= mb_substr($pad_string, 0, ($length - $len) % $pad_string_len);
158  return $string;
159  }
160  return $string;
161  }
162 
176  public static function ‪base64urlEncode(string $value): string
177  {
178  return strtr(base64_encode($value), ['+' => '-', '/' => '_', '=' => '']);
179  }
180 
195  public static function ‪base64urlDecode(string $value, bool $strict = false): string|false
196  {
197  return base64_decode(strtr($value, ['-' => '+', '_' => '/']), $strict);
198  }
199 
209  public static function ‪explodeEscaped(string $delimiter, string $subject, string $escapeCharacter = '\\'): array
210  {
211  if ($delimiter !== '') {
212  $placeholder = '\\0\\0\\0_esc';
213  $subjectEscaped = str_replace($escapeCharacter . $delimiter, $placeholder, $subject);
214  $escapeParts = explode($delimiter, $subjectEscaped);
215  foreach ($escapeParts as &$part) {
216  $part = str_replace($placeholder, $delimiter, $part);
217  }
218  return $escapeParts;
219  }
220  return [$subject];
221  }
222 }
‪TYPO3\CMS\Core\Utility\StringUtility\base64urlEncode
‪static string base64urlEncode(string $value)
Definition: StringUtility.php:176
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:18
‪TYPO3\CMS\Core\Utility\StringUtility\searchStringWildcard
‪static bool searchStringWildcard(string $haystack, string $needle)
Definition: StringUtility.php:95
‪TYPO3\CMS\Core\Utility\StringUtility\uniqueList
‪static string uniqueList(string $list)
Definition: StringUtility.php:122
‪TYPO3\CMS\Core\Utility\StringUtility\removeByteOrderMark
‪static removeByteOrderMark(string $input)
Definition: StringUtility.php:79
‪TYPO3\CMS\Core\Utility\StringUtility\escapeCssSelector
‪static escapeCssSelector(string $selector)
Definition: StringUtility.php:69
‪TYPO3\CMS\Core\Utility\StringUtility\cast
‪static cast(mixed $value, ?string $default=null)
Definition: StringUtility.php:30
‪TYPO3\CMS\Core\Utility\StringUtility\multibyteStringPad
‪static multibyteStringPad(string $string, int $length, string $pad_string=' ', int $pad_type=STR_PAD_RIGHT, string $encoding='UTF-8')
Definition: StringUtility.php:131
‪TYPO3\CMS\Core\Utility\StringUtility\explodeEscaped
‪static explodeEscaped(string $delimiter, string $subject, string $escapeCharacter='\\')
Definition: StringUtility.php:209
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Core\Utility\StringUtility\filter
‪static filter(mixed $value, ?string $default=null)
Definition: StringUtility.php:48
‪TYPO3\CMS\Core\Utility\StringUtility\base64urlDecode
‪static string false base64urlDecode(string $value, bool $strict=false)
Definition: StringUtility.php:195
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57