Skip to content

Commit add8dce

Browse files
committed
Add string methods missing in PHP7
1 parent 5466c2d commit add8dce

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/TgUtils/Utils.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,46 @@ public static function isEmail($s) {
130130
return filter_var($s, FILTER_VALIDATE_EMAIL);
131131
}
132132

133+
/**
134+
* NULL-safe startsWith test for strings.
135+
* @return boolean TRUE when the test succeeds, FALSE if any argument is NULL or test failed.
136+
*/
137+
public static function startsWith($haystack, $needle, $ignoreCase = FALSE) {
138+
if (($haystack == NULL) || ($needle == NULL)) return FALSE;
139+
if ($ignoreCase) {
140+
$haystack = mb_strtolower($haystack);
141+
$needle = mb_strtolower($needle);
142+
}
143+
return mb_strpos($haystack, $needle) === 0;
144+
}
145+
146+
/**
147+
* NULL-safe endsWith test for strings.
148+
* @return boolean TRUE when the test succeeds, FALSE if any argument is NULL or test failed.
149+
*/
150+
public static function endsWith($haystack, $needle, $ignoreCase = FALSE) {
151+
if (($haystack == NULL) || ($needle == NULL)) return FALSE;
152+
if ($ignoreCase) {
153+
$haystack = mb_strtolower($haystack);
154+
$needle = mb_strtolower($needle);
155+
}
156+
return mb_strpos($haystack, $needle) == mb_strlen($haystack)-mb_strlen($needle);
157+
}
158+
159+
/**
160+
* NULL-safe contains test for strings.
161+
* @return boolean TRUE when the test succeeds, FALSE if any argument is NULL or test failed.
162+
*/
163+
public static function contains($haystack, $needle, $ignoreCase = FALSE) {
164+
if (($haystack == NULL) || ($needle == NULL)) return FALSE;
165+
if ($ignoreCase) {
166+
$haystack = mb_strtolower($haystack);
167+
$needle = mb_strtolower($needle);
168+
}
169+
return mb_strpos($haystack, $needle) !== FALSE;
170+
}
171+
172+
/**
133173
/**
134174
* Checks whether an array is associative or not.
135175
* @param array $arr - the array to be tested

0 commit comments

Comments
 (0)