Last active
April 14, 2024 22:24
-
-
Save juliyvchirkov/8f325f9ac534fe736b504b93a1a8b2ce to your computer and use it in GitHub Desktop.
php: polyfills of string functions str_starts_with, str_contains and str_ends_with
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types = 1); | |
/** | |
* Provides polyfills of string functions str_starts_with, str_contains and str_ends_with, | |
* core functions since PHP 8, along with their multibyte implementations mb_str_starts_with, | |
* mb_str_contains and mb_str_ends_with | |
* | |
* Covers PHP 4 - PHP 7, safe to utilize with PHP 8 | |
*/ | |
/** | |
* @see https://www.php.net/manual/en/function.str-starts-with | |
*/ | |
if (!function_exists('str_starts_with')) { | |
function str_starts_with(string $haystack, string $needle): bool | |
{ | |
return strlen($needle) === 0 || strpos($haystack, $needle) === 0; | |
} | |
} | |
/** | |
* @see https://www.php.net/manual/en/function.str-contains | |
*/ | |
if (!function_exists('str_contains')) { | |
function str_contains(string $haystack, string $needle): bool | |
{ | |
return strlen($needle) === 0 || strpos($haystack, $needle) !== false; | |
} | |
} | |
/** | |
* @see https://www.php.net/manual/en/function.str-ends-with | |
*/ | |
if (!function_exists('str_ends_with')) { | |
function str_ends_with(string $haystack, string $needle): bool | |
{ | |
return strlen($needle) === 0 || substr($haystack, -strlen($needle)) === $needle; | |
} | |
} | |
if (!function_exists('mb_str_starts_with')) { | |
function mb_str_starts_with(string $haystack, string $needle): bool | |
{ | |
return mb_strlen($needle) === 0 || mb_strpos($haystack, $needle) === 0; | |
} | |
} | |
if (!function_exists('mb_str_contains')) { | |
function mb_str_contains(string $haystack, string $needle): bool | |
{ | |
return mb_strlen($needle) === 0 || mb_strpos($haystack, $needle) !== false; | |
} | |
} | |
if (!function_exists('mb_str_ends_with')) { | |
function mb_str_ends_with(string $haystack, string $needle): bool | |
{ | |
return mb_strlen($needle) === 0 || mb_substr($haystack, -mb_strlen($needle)) === $needle; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@nextgenthemes Thanks for your review!
C'mon, it's just a gist. Sure thing
PHP 4
provides no support for type hints. Remove the type hints from the code and you'll achieve the goal.That simple.
https://onlinephp.io/c/9bbc68fa-4065-43ac-800b-072275ce4b74
Thank you, I'm utilizing
Symfony Polyfill
myself if available. The code of these guys is implemented in a strict clearSymphony way
and I like and respect it.Mine simplified alternatives shared above have been developed the day after the release of
PHP 8
as temporary solution tillSymfony
delivers their polyfills forPHP 8
, and as soon assymfony/polyfill-php80
bundle arrived, I switched to it.But, long story short, the fact that
Symfony
no doubt implements top quality solutions doesn't mean my alternatives don't serve their job.C'mon, the preamble at the gist header comment notes clear and crisp «polyfills of string functions str_starts_with, str_contains and str_ends_with, core functions since PHP 8, along with their multibyte implementations». Period.