note 130624 added to function.str-contains
| From: | vinature.vine@gmail.com | Date: | Sat, 24 Jan 2026 13:45:18 +0000 |
| Subject: | note 130624 added to function.str-contains | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-222514@lists.php.net to get a copy of this message | ||
Here is a class that extends the functionality of the string "str_contains",
"str_ends_with", and "str_starts_with". It uses "array_any" and
"array_all", which require PHP >= 8.4.0, but can be easily polyfilled.
<?php
namespace Vinature;
final class StrCheck {
static function str_starts_with_all(string $haystack, array $needles): bool {
return \array_all($needles, fn($needle) => \str_starts_with($haystack, $needle));
}
static function str_starts_with_any(string $haystack, array $needles): bool {
return \array_any($needles, fn($needle) => \str_starts_with($haystack, $needle));
}
static function str_ends_with_all(string $haystack, array $needles): bool {
return \array_all($needles, fn($needle) => \str_ends_with($haystack, $needle));
}
static function str_ends_with_any(string $haystack, array $needles): bool {
return \array_any($needles, fn($needle) => \str_ends_with($haystack, $needle));
}
static function str_contains_all(string $haystack, array $needles): bool {
return \array_all($needles, fn($needle) => \str_contains($haystack, $needle));
}
static function str_contains_any(string $haystack, array $needles): bool {
return \array_any($needles, fn($needle) => \str_contains($haystack, $needle));
}
}
?>
And this is how to use this Class.
<?php
use \Vinature\StrCheck;
$str = 'Je mange un plat de Koki avec du plantains mûrs. I\'m eating a plate of Koki
beans and ripe plantains.';
$results = [];
$results['str_starts_with_any'] = [];
$results['str_starts_with_any'][] = StrCheck::str_starts_with_any($str, ['Je
mange','plat']);
$results['str_starts_with_any'][] = StrCheck::str_starts_with_any($str,
['plantains','mûrs']);
$results['str_ends_with_all'] = [];
$results['str_ends_with_all'][] = StrCheck::str_ends_with_all($str,
['plantains.','.']);
$results['str_ends_with_all'][] = StrCheck::str_ends_with_all($str,
['steak','plantains']);
$results['str_contains_any'] = [];
$results['str_contains_any'][] = StrCheck::str_contains_any($str,
['Koki','mûrs']);
$results['str_contains_any'][] = StrCheck::str_contains_any($str,
['steak','sandwich']);
print_r($results);
?>
The output should looks like:
[
'str_starts_with_any' => [
0 => true,
1 => false,
],
'str_ends_with_all' => [
0 => true,
1 => false,
],
'str_contains_any' => [
0 => true,
1 => false,
]
]
----
Server IP: 206.189.2.9
Probable Submitter: 154.72.153.248
----
Manual Page -- https://php.net/manual/en/function.str-contains.php
Edit -- https://main.php.net/note/edit/130624
Del: integrated -- https://main.php.net/note/delete/130624/integrated
Del: useless -- https://main.php.net/note/delete/130624/useless
Del: bad code -- https://main.php.net/note/delete/130624/bad+code
Del: spam -- https://main.php.net/note/delete/130624/spam
Del: non-english -- https://main.php.net/note/delete/130624/non-english
Del: in docs -- https://main.php.net/note/delete/130624/in+docs
Del: other reasons-- https://main.php.net/note/delete/130624
Reject -- https://main.php.net/note/reject/130624
Search -- https://main.php.net/manage/user-notes.php