‪TYPO3CMS  9.5
StringUtility.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 {
31  public static function ‪beginsWith($haystack, $needle)
32  {
33  // Sanitize $haystack and $needle
34  if (is_array($haystack) || is_object($haystack) || $haystack === null || (string)$haystack != $haystack) {
35  throw new \InvalidArgumentException(
36  '$haystack can not be interpreted as string',
37  1347135546
38  );
39  }
40  if (is_array($needle) || is_object($needle) || (string)$needle != $needle || strlen($needle) < 1) {
41  throw new \InvalidArgumentException(
42  '$needle can not be interpreted as string or has zero length',
43  1347135547
44  );
45  }
46  $haystack = (string)$haystack;
47  $needle = (string)$needle;
48  return $needle !== '' && strpos($haystack, $needle) === 0;
49  }
50 
60  public static function ‪endsWith($haystack, $needle)
61  {
62  // Sanitize $haystack and $needle
63  if (is_array($haystack) || is_object($haystack) || $haystack === null || (string)$haystack != $haystack) {
64  throw new \InvalidArgumentException(
65  '$haystack can not be interpreted as string',
66  1347135544
67  );
68  }
69  if (is_array($needle) || is_object($needle) || (string)$needle != $needle || strlen($needle) < 1) {
70  throw new \InvalidArgumentException(
71  '$needle can not be interpreted as string or has no length',
72  1347135545
73  );
74  }
75  $haystackLength = strlen($haystack);
76  $needleLength = strlen($needle);
77  if (!$haystackLength || $needleLength > $haystackLength) {
78  return false;
79  }
80  $position = strrpos((string)$haystack, (string)$needle);
81  return $position !== false && $position === $haystackLength - $needleLength;
82  }
83 
91  public static function ‪getUniqueId($prefix = '')
92  {
93  $uniqueId = uniqid($prefix, true);
94  return str_replace('.', '', $uniqueId);
95  }
96 
106  public static function ‪escapeCssSelector(string $selector): string
107  {
108  return preg_replace('/([#:.\\[\\],=@])/', '\\\\$1', $selector);
109  }
110 
117  public static function ‪removeByteOrderMark(string $input): string
118  {
119  if (strpos($input, "\xef\xbb\xbf") === 0) {
120  $input = substr($input, 3);
121  }
122 
123  return $input;
124  }
125 
133  public static function ‪searchStringWildcard($haystack, $needle): bool
134  {
135  $result = false;
136  if ($haystack === $needle) {
137  $result = true;
138  } elseif ($needle) {
139  if (preg_match('/^\\/.+\\/$/', $needle)) {
140  // Regular expression, only "//" is allowed as delimiter
141  $regex = $needle;
142  } else {
143  $needle = str_replace(['*', '?'], ['%%%MANY%%%', '%%%ONE%%%'], $needle);
144  $regex = '/^' . preg_quote($needle, '/') . '$/';
145  // Replace the marker with .* to match anything (wildcard)
146  $regex = str_replace(['%%%MANY%%%', '%%%ONE%%%'], ['.*', '.'], $regex);
147  }
148  $result = (bool)preg_match($regex, $haystack);
149  }
150  return $result;
151  }
152 }
‪TYPO3\CMS\Core\Utility\StringUtility\searchStringWildcard
‪static bool searchStringWildcard($haystack, $needle)
Definition: StringUtility.php:133
‪TYPO3\CMS\Core\Utility\StringUtility\endsWith
‪static bool endsWith($haystack, $needle)
Definition: StringUtility.php:60
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:2
‪TYPO3\CMS\Core\Utility\StringUtility\escapeCssSelector
‪static string escapeCssSelector(string $selector)
Definition: StringUtility.php:106
‪TYPO3\CMS\Core\Utility\StringUtility\beginsWith
‪static bool beginsWith($haystack, $needle)
Definition: StringUtility.php:31
‪TYPO3\CMS\Core\Utility\StringUtility\removeByteOrderMark
‪static string removeByteOrderMark(string $input)
Definition: StringUtility.php:117
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:91
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:21