e4ee84504b17e794114b2de9873f2a71c63b4427
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / StringUtility.php
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
17 /**
18 * Class with helper functions for string handling
19 */
20 class StringUtility
21 {
22 /**
23 * Returns TRUE if $haystack ends with $needle.
24 * The input string is not trimmed before and search
25 * is done case sensitive.
26 *
27 * @param string $haystack Full string to check
28 * @param string $needle Reference string which must be found as the "last part" of the full string
29 * @throws \InvalidArgumentException
30 * @return bool TRUE if $needle was found to be equal to the last part of $str
31 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use endsWith() instead
32 */
33 public static function isLastPartOfString($haystack, $needle)
34 {
35 GeneralUtility::logDeprecatedFunction();
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
54 /**
55 * Returns TRUE if $haystack begins with $needle.
56 * The input string is not trimmed before and search is done case sensitive.
57 *
58 * @param string $haystack Full string to check
59 * @param string $needle Reference string which must be found as the "first part" of the full string
60 * @throws \InvalidArgumentException
61 * @return bool TRUE if $needle was found to be equal to the first part of $haystack
62 */
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
83 /**
84 * Returns TRUE if $haystack ends with $needle.
85 * The input string is not trimmed before and search is done case sensitive.
86 *
87 * @param string $haystack Full string to check
88 * @param string $needle Reference string which must be found as the "last part" of the full string
89 * @throws \InvalidArgumentException
90 * @return bool TRUE if $needle was found to be equal to the last part of $haystack
91 */
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
116 /**
117 * This function generates a unique id by using the more entropy parameter.
118 * Furthermore the dots are removed so the id can be used inside HTML attributes e.g. id.
119 *
120 * @param string $prefix
121 * @return string
122 */
123 public static function getUniqueId($prefix = '')
124 {
125 $uniqueId = uniqid($prefix, true);
126 return str_replace('.', '', $uniqueId);
127 }
128 }