TYPO3 CMS  TYPO3_7-6
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 {
33  public static function isLastPartOfString($haystack, $needle)
34  {
36  // Sanitize $haystack and $needle
37  if (is_object($haystack) || (string)$haystack != $haystack || strlen($haystack) < 1) {
38  throw new \InvalidArgumentException(
39  '$haystack can not be interpreted as string or has no length',
40  1347135544
41  );
42  }
43  if (is_object($needle) || (string)$needle != $needle || strlen($needle) < 1) {
44  throw new \InvalidArgumentException(
45  '$needle can not be interpreted as string or has no length',
46  1347135545
47  );
48  }
49  $stringLength = strlen($haystack);
50  $needleLength = strlen($needle);
51  return strrpos((string)$haystack, (string)$needle, 0) === $stringLength - $needleLength;
52  }
53 
63  public static function beginsWith($haystack, $needle)
64  {
65  // Sanitize $haystack and $needle
66  if (is_object($haystack) || $haystack === null || (string)$haystack != $haystack) {
67  throw new \InvalidArgumentException(
68  '$haystack can not be interpreted as string',
69  1347135546
70  );
71  }
72  if (is_object($needle) || (string)$needle != $needle || strlen($needle) < 1) {
73  throw new \InvalidArgumentException(
74  '$needle can not be interpreted as string or has zero length',
75  1347135547
76  );
77  }
78  $haystack = (string)$haystack;
79  $needle = (string)$needle;
80  return $needle !== '' && strpos($haystack, $needle) === 0;
81  }
82 
92  public static function endsWith($haystack, $needle)
93  {
94  // Sanitize $haystack and $needle
95  if (is_object($haystack) || $haystack === null || (string)$haystack != $haystack) {
96  throw new \InvalidArgumentException(
97  '$haystack can not be interpreted as string',
98  1347135544
99  );
100  }
101  if (is_object($needle) || (string)$needle != $needle || strlen($needle) < 1) {
102  throw new \InvalidArgumentException(
103  '$needle can not be interpreted as string or has no length',
104  1347135545
105  );
106  }
107  $haystackLength = strlen($haystack);
108  $needleLength = strlen($needle);
109  if (!$haystackLength || $needleLength > $haystackLength) {
110  return false;
111  }
112  $position = strrpos((string)$haystack, (string)$needle);
113  return $position !== false && $position === $haystackLength - $needleLength;
114  }
115 
123  public static function getUniqueId($prefix = '')
124  {
125  $uniqueId = uniqid($prefix, true);
126  return str_replace('.', '', $uniqueId);
127  }
128 }
static isLastPartOfString($haystack, $needle)
static beginsWith($haystack, $needle)
static endsWith($haystack, $needle)