case insensitive string functions

This commit is contained in:
Roman 2021-04-03 11:07:56 +02:00
parent 18e7955b12
commit 98916e0b23

@ -26,18 +26,45 @@ function generateRandomString($length): string {
return $randomString; return $randomString;
} }
function startsWith($haystack, $needle): bool { function startsWith($haystack, $needle, bool $ignoreCase = false): bool {
$length = strlen($needle); $length = strlen($needle);
return (substr($haystack, 0, $length) === $needle); if ($length === 0) {
return true;
} }
function endsWith($haystack, $needle): bool { if ($ignoreCase) {
$length = strlen($needle); $haystack = strtolower($haystack);
if ($length == 0) $needle = strtolower($haystack);
return true; }
// PHP 8.0 support
if (function_exists("str_starts_with")) {
return str_starts_with($haystack, $needle);
} else {
return (substr($haystack, 0, $length) === $needle);
}
}
function endsWith($haystack, $needle, bool $ignoreCase = false): bool {
$length = strlen($needle);
if ($length === 0) {
return true;
}
if ($ignoreCase) {
$haystack = strtolower($haystack);
$needle = strtolower($haystack);
}
// PHP 8.0 support
if (function_exists("str_ends_with")) {
return str_ends_with($haystack, $needle);
} else {
return (substr($haystack, -$length) === $needle); return (substr($haystack, -$length) === $needle);
} }
}
function intendCode($code, $escape = true) { function intendCode($code, $escape = true) {
$newCode = ""; $newCode = "";