Re: Can we make str_(starts|ends)_with variadic?
| From: | Tim Düsterhus | Date: | Wed, 11 Jun 2025 10:20:40 +0000 |
| Subject: | Re: Can we make str_(starts|ends)_with variadic? | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-127647@lists.php.net to get a copy of this message | ||
Hi
Am 2025-06-11 11:58, schrieb Christian Schneider:
Am 10.06.2025 um 21:46 schrieb Hans Henrik Bergan <hans@loltek.net>:I agree that the examples are not particularly well-chosen, because the correct implementation for the two examples would be:Can we make str_(starts|ends)_with variadic?[…] Which leaves me +-0 on this, - Chrisif (str_starts_with($url, "http://", "https://")) { // url } if (str_ends_with($filename, ".pdf", ".doc", ".docx")) { // document } if(str_ends_with($str, ...$validExtensions){ // valid extension }
if (\in_array(\pathinfo($filename, PATHINFO_EXTENSION), ['pdf', 'doc', 'docx'], true)) { }
and
if (\in_array(\Uri\Rfc3986\Uri::parse($url)?->getScheme(), ['http', 'https'], true)) { }
and then leaving it up to the code developers / the engien to possibly optimize \in_array() into match() or similar when only a small number of values need to be matched.
Best regards
Tim Düsterhus