From 98916e0b239801482c6f8c1595e9de8769c09a0b Mon Sep 17 00:00:00 2001 From: Roman Date: Sat, 3 Apr 2021 11:07:56 +0200 Subject: [PATCH] case insensitive string functions --- core/core.php | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/core/core.php b/core/core.php index e078302..09daa68 100644 --- a/core/core.php +++ b/core/core.php @@ -26,17 +26,44 @@ function generateRandomString($length): string { return $randomString; } -function startsWith($haystack, $needle): bool { +function startsWith($haystack, $needle, bool $ignoreCase = false): bool { + $length = strlen($needle); - return (substr($haystack, 0, $length) === $needle); + if ($length === 0) { + return true; + } + + if ($ignoreCase) { + $haystack = strtolower($haystack); + $needle = strtolower($haystack); + } + + // 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 { - $length = strlen($needle); - if ($length == 0) - return true; +function endsWith($haystack, $needle, bool $ignoreCase = false): bool { - return (substr($haystack, -$length) === $needle); + $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); + } } function intendCode($code, $escape = true) {