polyfills for php8's str_starts_with and str_ends_with

This commit is contained in:
Shish
2020-10-25 19:15:13 +00:00
parent 4ac9ab2ad6
commit c783ff0e8d
4 changed files with 21 additions and 18 deletions

View File

@@ -341,17 +341,20 @@ function unparse_url($parsed_url)
return "$scheme$user$pass$host$port$path$query$fragment";
}
function startsWith(string $haystack, string $needle): bool
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
# finally in the core library starting from php8
if (!function_exists('str_starts_with')) {
function str_starts_with(string $haystack, string $needle): bool
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
function endsWith(string $haystack, string $needle): bool
{
$length = strlen($needle);
$start = $length * -1; //negative
return (substr($haystack, $start) === $needle);
function str_ends_with(string $haystack, string $needle): bool
{
$length = strlen($needle);
$start = $length * -1; //negative
return (substr($haystack, $start) === $needle);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\