‪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 {
29  public static function ‪getUniqueId(string $prefix = ''): string
30  {
31  $uniqueId = uniqid($prefix, true);
32  return str_replace('.', '', $uniqueId);
33  }
34 
41  public static function ‪escapeCssSelector(string $selector): string
42  {
43  return preg_replace('/([#:.\\[\\],=@])/', '\\\\$1', $selector);
44  }
45 
51  public static function ‪removeByteOrderMark(string $input): string
52  {
53  if (str_starts_with($input, "\xef\xbb\xbf")) {
54  $input = substr($input, 3);
55  }
56 
57  return $input;
58  }
59 
67  public static function ‪searchStringWildcard(string $haystack, string $needle): bool
68  {
69  $result = false;
70  if ($haystack === $needle) {
71  $result = true;
72  } elseif ($needle) {
73  if (preg_match('/^\\/.+\\/$/', $needle)) {
74  // Regular expression, only "//" is allowed as delimiter
75  $regex = $needle;
76  } else {
77  $needle = str_replace(['*', '?'], ['%%%MANY%%%', '%%%ONE%%%'], $needle);
78  $regex = '/^' . preg_quote($needle, '/') . '$/';
79  // Replace the marker with .* to match anything (wildcard)
80  $regex = str_replace(['%%%MANY%%%', '%%%ONE%%%'], ['.*', '.'], $regex);
81  }
82  $result = (bool)preg_match($regex, $haystack);
83  }
84  return $result;
85  }
86 
94  public static function ‪uniqueList(string $list): string
95  {
96  return implode(',', array_unique(‪GeneralUtility::trimExplode(',', $list, true)));
97  }
98 
103  public static function ‪multibyteStringPad(string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, string $encoding = 'UTF-8'): string
104  {
105  $len = mb_strlen($string, $encoding);
106  $pad_string_len = mb_strlen($pad_string, $encoding);
107  if ($len >= $length || $pad_string_len === 0) {
108  return $string;
109  }
110 
111  switch ($pad_type) {
112  case STR_PAD_RIGHT:
113  $string .= str_repeat($pad_string, (int)(($length - $len)/$pad_string_len));
114  $string .= mb_substr($pad_string, 0, ($length - $len) % $pad_string_len);
115  return $string;
116 
117  case STR_PAD_LEFT:
118  $leftPad = str_repeat($pad_string, (int)(($length - $len)/$pad_string_len));
119  $leftPad .= mb_substr($pad_string, 0, ($length - $len) % $pad_string_len);
120  return $leftPad . $string;
121 
122  case STR_PAD_BOTH:
123  $leftPadCount = (int)(($length - $len)/2);
124  $len += $leftPadCount;
125  $padded = ((int)($leftPadCount / $pad_string_len)) * $pad_string_len;
126  $leftPad = str_repeat($pad_string, (int)($leftPadCount / $pad_string_len));
127  $leftPad .= mb_substr($pad_string, 0, $leftPadCount - $padded);
128  $string = $leftPad . $string . str_repeat($pad_string, (int)(($length - $len)/$pad_string_len));
129  $string .= mb_substr($pad_string, 0, ($length - $len) % $pad_string_len);
130  return $string;
131  }
132  return $string;
133  }
134 
148  public static function ‪base64urlEncode(string $value): string
149  {
150  return strtr(base64_encode($value), ['+' => '-', '/' => '_', '=' => '']);
151  }
152 
166  public static function ‪base64urlDecode(string $value): string
167  {
168  return base64_decode(strtr($value, ['-' => '+', '_' => '/']));
169  }
170 
180  public static function ‪explodeEscaped(string $delimiter, string $subject, string $escapeCharacter = '\\'): array
181  {
182  if ($delimiter !== '') {
183  $placeholder = '\\0\\0\\0_esc';
184  $subjectEscaped = str_replace($escapeCharacter . $delimiter, $placeholder, $subject);
185  $escapeParts = explode($delimiter, $subjectEscaped);
186  foreach ($escapeParts as &$part) {
187  $part = str_replace($placeholder, $delimiter, $part);
188  }
189  return $escapeParts;
190  }
191  return [$subject];
192  }
193 }
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:916
‪TYPO3\CMS\Core\Utility\StringUtility\base64urlEncode
‪static string base64urlEncode(string $value)
Definition: StringUtility.php:148
‪TYPO3\CMS\Core\Utility\StringUtility\base64urlDecode
‪static string base64urlDecode(string $value)
Definition: StringUtility.php:166
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:18
‪TYPO3\CMS\Core\Utility\StringUtility\searchStringWildcard
‪static bool searchStringWildcard(string $haystack, string $needle)
Definition: StringUtility.php:67
‪TYPO3\CMS\Core\Utility\StringUtility\uniqueList
‪static string uniqueList(string $list)
Definition: StringUtility.php:94
‪TYPO3\CMS\Core\Utility\StringUtility\removeByteOrderMark
‪static removeByteOrderMark(string $input)
Definition: StringUtility.php:51
‪TYPO3\CMS\Core\Utility\StringUtility\escapeCssSelector
‪static escapeCssSelector(string $selector)
Definition: StringUtility.php:41
‪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:103
‪TYPO3\CMS\Core\Utility\StringUtility\explodeEscaped
‪static explodeEscaped(string $delimiter, string $subject, string $escapeCharacter='\\')
Definition: StringUtility.php:180
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:29