‪TYPO3CMS  11.5
StringUtility.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 {
30  public static function ‪cast($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 
50  public static function ‪filter($value, ?string $default = null): ?string
51  {
52  return is_string($value) ? $value : $default;
53  }
54 
65  public static function ‪beginsWith($haystack, $needle)
66  {
67  trigger_error('StringUtility::beginsWith() will be removed in TYPO3 v12.0. Use PHPs str_starts_with() function instead.', E_USER_DEPRECATED);
68  // Sanitize $haystack and $needle
69  if (is_array($haystack) || is_object($haystack) || $haystack === null || (string)$haystack != $haystack) {
70  throw new \InvalidArgumentException(
71  '$haystack can not be interpreted as string',
72  1347135546
73  );
74  }
75  if (is_array($needle) || is_object($needle) || (string)$needle != $needle || strlen($needle) < 1) {
76  throw new \InvalidArgumentException(
77  '$needle can not be interpreted as string or has zero length',
78  1347135547
79  );
80  }
81  $haystack = (string)$haystack;
82  $needle = (string)$needle;
83  return $needle !== '' && strpos($haystack, $needle) === 0;
84  }
85 
96  public static function ‪endsWith($haystack, $needle)
97  {
98  trigger_error('StringUtility::endsWith() will be removed in TYPO3 v12.0. Use PHPs str_ends_with() function instead.', E_USER_DEPRECATED);
99  // Sanitize $haystack and $needle
100  if (is_array($haystack) || is_object($haystack) || $haystack === null || (string)$haystack != $haystack) {
101  throw new \InvalidArgumentException(
102  '$haystack can not be interpreted as string',
103  1347135544
104  );
105  }
106  if (is_array($needle) || is_object($needle) || (string)$needle != $needle || strlen($needle) < 1) {
107  throw new \InvalidArgumentException(
108  '$needle can not be interpreted as string or has no length',
109  1347135545
110  );
111  }
112  $haystackLength = strlen($haystack);
113  $needleLength = strlen($needle);
114  if (!$haystackLength || $needleLength > $haystackLength) {
115  return false;
116  }
117  $position = strrpos((string)$haystack, (string)$needle);
118  return $position !== false && $position === $haystackLength - $needleLength;
119  }
120 
128  public static function ‪getUniqueId($prefix = '')
129  {
130  $uniqueId = uniqid($prefix, true);
131  return str_replace('.', '', $uniqueId);
132  }
133 
143  public static function ‪escapeCssSelector(string $selector): string
144  {
145  return preg_replace('/([#:.\\[\\],=@])/', '\\\\$1', $selector);
146  }
147 
154  public static function ‪removeByteOrderMark(string $input): string
155  {
156  if (strpos($input, "\xef\xbb\xbf") === 0) {
157  $input = substr($input, 3);
158  }
159 
160  return $input;
161  }
162 
170  public static function ‪searchStringWildcard($haystack, $needle): bool
171  {
172  $result = false;
173  if ($haystack === $needle) {
174  $result = true;
175  } elseif ($needle) {
176  if (preg_match('/^\\/.+\\/$/', $needle)) {
177  // Regular expression, only "//" is allowed as delimiter
178  $regex = $needle;
179  } else {
180  $needle = str_replace(['*', '?'], ['%%%MANY%%%', '%%%ONE%%%'], $needle);
181  $regex = '/^' . preg_quote($needle, '/') . '$/';
182  // Replace the marker with .* to match anything (wildcard)
183  $regex = str_replace(['%%%MANY%%%', '%%%ONE%%%'], ['.*', '.'], $regex);
184  }
185  $result = (bool)preg_match($regex, $haystack);
186  }
187  return $result;
188  }
189 
197  public static function ‪uniqueList(string $list): string
198  {
199  return implode(',', array_unique(‪GeneralUtility::trimExplode(',', $list, true)));
200  }
201 
213  public static function ‪multibyteStringPad(string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, string $encoding = 'UTF-8'): string
214  {
215  $len = mb_strlen($string, $encoding);
216  $pad_string_len = mb_strlen($pad_string, $encoding);
217  if ($len >= $length || $pad_string_len === 0) {
218  return $string;
219  }
220 
221  switch ($pad_type) {
222  case STR_PAD_RIGHT:
223  $string .= str_repeat($pad_string, (int)(($length - $len) / $pad_string_len));
224  $string .= mb_substr($pad_string, 0, ($length - $len) % $pad_string_len);
225  return $string;
226 
227  case STR_PAD_LEFT:
228  $leftPad = str_repeat($pad_string, (int)(($length - $len) / $pad_string_len));
229  $leftPad .= mb_substr($pad_string, 0, ($length - $len) % $pad_string_len);
230  return $leftPad . $string;
231 
232  case STR_PAD_BOTH:
233  $leftPadCount = (int)(($length - $len) / 2);
234  $len += $leftPadCount;
235  $padded = ((int)($leftPadCount / $pad_string_len)) * $pad_string_len;
236  $leftPad = str_repeat($pad_string, (int)($leftPadCount / $pad_string_len));
237  $leftPad .= mb_substr($pad_string, 0, $leftPadCount - $padded);
238  $string = $leftPad . $string . str_repeat($pad_string, (int)(($length - $len) / $pad_string_len));
239  $string .= mb_substr($pad_string, 0, ($length - $len) % $pad_string_len);
240  return $string;
241  }
242  return $string;
243  }
244 
258  public static function ‪base64urlEncode(string $value): string
259  {
260  return strtr(base64_encode($value), ['+' => '-', '/' => '_', '=' => '']);
261  }
262 
276  public static function ‪base64urlDecode(string $value): string
277  {
278  return base64_decode(strtr($value, ['-' => '+', '_' => '/']));
279  }
280 }
‪TYPO3\CMS\Core\Utility\StringUtility\searchStringWildcard
‪static bool searchStringWildcard($haystack, $needle)
Definition: StringUtility.php:170
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:999
‪TYPO3\CMS\Core\Utility\StringUtility\base64urlEncode
‪static string base64urlEncode(string $value)
Definition: StringUtility.php:258
‪TYPO3\CMS\Core\Utility\StringUtility\endsWith
‪static bool endsWith($haystack, $needle)
Definition: StringUtility.php:96
‪TYPO3\CMS\Core\Utility\StringUtility\base64urlDecode
‪static string base64urlDecode(string $value)
Definition: StringUtility.php:276
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:16
‪TYPO3\CMS\Core\Utility\StringUtility\uniqueList
‪static string uniqueList(string $list)
Definition: StringUtility.php:197
‪TYPO3\CMS\Core\Utility\StringUtility\escapeCssSelector
‪static string escapeCssSelector(string $selector)
Definition: StringUtility.php:143
‪TYPO3\CMS\Core\Utility\StringUtility\beginsWith
‪static bool beginsWith($haystack, $needle)
Definition: StringUtility.php:65
‪TYPO3\CMS\Core\Utility\StringUtility\removeByteOrderMark
‪static string removeByteOrderMark(string $input)
Definition: StringUtility.php:154
‪TYPO3\CMS\Core\Utility\StringUtility\filter
‪static filter($value, ?string $default=null)
Definition: StringUtility.php:50
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:128
‪TYPO3\CMS\Core\Utility\StringUtility\cast
‪static cast($value, ?string $default=null)
Definition: StringUtility.php:30
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Utility\StringUtility\multibyteStringPad
‪static string multibyteStringPad(string $string, int $length, string $pad_string=' ', int $pad_type=STR_PAD_RIGHT, string $encoding='UTF-8')
Definition: StringUtility.php:213