[php-src] PR #18825: str_starts_with variadic needle
| From: | divinity76 | Date: | Tue, 10 Jun 2025 15:27:11 +0000 |
| Subject: | [php-src] PR #18825: str_starts_with variadic needle | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-32053@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/18825
Author: divinity76
make str_starts_with and str_ends_with variadic:
```php
str_starts_with(string $haystack, string ...$needle): bool
str_ends_with(string $haystack, string ...$needle): bool
```
a simple example could be
```php
if (str_starts_with($url, "http://", "https://")) {
// url
}
if (str_ends_with($filename, ".pdf", ".doc", ".docx")) {
// document
}
if(str_starts_with($string ...$arr){
//
}
```
which seems much easier than
```php
if (str_starts_with($url, "http://") || str_starts_with($url,
"https://")) {
// url
}
if (str_ends_with($filename, ".pdf") || str_ends_with($filename, ".doc") ||
str_ends_with($filename, ".docx")) {
// document
}
$found = false;
foreach ($arr as $needle) {
if (str_starts_with($string, $needle)) {
$found = true;
break;
}
}
if ($found) {
//
}
```
inspired by Python's
str.startswith() which supports python-tuples like if
str.startswith(("foo","bar")): ...