note 128933 added to function.str-starts-with
| From: | JacobSlomp at main dot php dot net | Date: | Thu, 05 Oct 2023 17:04:49 +0000 |
| Subject: | note 128933 added to function.str-starts-with | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220892@lists.php.net to get a copy of this message | ||
In the previous provided function you will get an error if the needle is longer then the haystack.
So here is an update;
<?php
if (!function_exists('str_starts_with')) {
function str_starts_with($haystack, $needle) {
if(strlen($haystack) < strlen($needle)){
return false;
}
return (string)$needle !== '' && strncmp($haystack, $needle,
strlen($needle)) === 0;
}
}
if (!function_exists('str_ends_with')) {
function str_ends_with($haystack, $needle) {
if(strlen($haystack) < strlen($needle)){
return false;
}
return $needle !== '' && substr($haystack, -strlen($needle)) ===
(string)$needle;
}
}
if (!function_exists('str_contains')) {
function str_contains($haystack, $needle) {
if(strlen($haystack) < strlen($needle)){
return false;
}
return $needle !== '' && mb_strpos($haystack, $needle) !== false;
}
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 173.183.183.60)
----
Manual Page -- https://php.net/manual/en/function.str-starts-with.php
Edit -- https://main.php.net/note/edit/128933
Del: integrated -- https://main.php.net/note/delete/128933/integrated
Del: useless -- https://main.php.net/note/delete/128933/useless
Del: bad code -- https://main.php.net/note/delete/128933/bad+code
Del: spam -- https://main.php.net/note/delete/128933/spam
Del: non-english -- https://main.php.net/note/delete/128933/non-english
Del: in docs -- https://main.php.net/note/delete/128933/in+docs
Del: other reasons-- https://main.php.net/note/delete/128933
Reject -- https://main.php.net/note/reject/128933
Search -- https://main.php.net/manage/user-notes.php