TYPO3 CMS  TYPO3_8-7
StringUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
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_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_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_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_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 }
static removeByteOrderMark(string $input)
static escapeCssSelector(string $selector)
static beginsWith($haystack, $needle)
static endsWith($haystack, $needle)