Can we make str_(starts|ends)_with variadic?
PR: str_(starts|ends)_with variadic needle by divinity76 · Pull Request #18825 · php/php-src · GitHub
Code like
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
}
$isValidExtension = false;
foreach ($validExtensions as $needle) {
if (str_ends_with($str, $needle)) {
$isValidExtension = true;
break;
}
}
if ($isValidExtension) {
// valid extension
}
could then be replaced with
if (str_starts_with($url, "http://", "https://")) {
// url
}
if (str_ends_with($filename, ".pdf", ".doc", ".docx")) {
// document
}
if(str_ends_with($str, ...$validExtensions){
// valid extension
}
Fwiw Python support str.endswith((".pdf", ".doc", ".docx"))