note 128796 added to function.str-contains
| From: | harl at gmail dot com | Date: | Sat, 12 Aug 2023 23:23:44 +0000 |
| Subject: | note 128796 added to function.str-contains | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220756@lists.php.net to get a copy of this message | ||
A couple of functions for checking if a string contains any of the strings in an array, or all of
the strings in an array:
<?php
function str_contains_any(string $haystack, array $needles): bool
{
return array_reduce($needles, fn($a, $n) => $a || str_contains($haystack, $n), false);
}
function str_contains_all(string $haystack, array $needles): bool
{
return array_reduce($needles, fn($a, $n) => $a && str_contains($haystack, $n), true);
}
?>
str_contains_all() will return true if $needles is an empty array. If you think that's wrong,
show me a string in $needles that DOESN'T appear in the $haystack, and then look up
"vacuous truth".
(By swapping haystacks and needles in the body of these functions you can create versions that check
if a needle appears in any/all of an array of haystacks.)
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 222.153.168.209)
----
Manual Page -- https://php.net/manual/en/function.str-contains.php
Edit -- https://main.php.net/note/edit/128796
Del: integrated -- https://main.php.net/note/delete/128796/integrated
Del: useless -- https://main.php.net/note/delete/128796/useless
Del: bad code -- https://main.php.net/note/delete/128796/bad+code
Del: spam -- https://main.php.net/note/delete/128796/spam
Del: non-english -- https://main.php.net/note/delete/128796/non-english
Del: in docs -- https://main.php.net/note/delete/128796/in+docs
Del: other reasons-- https://main.php.net/note/delete/128796
Reject -- https://main.php.net/note/reject/128796
Search -- https://main.php.net/manage/user-notes.php