Re: [RFC] [VOTE] Prefix and Suffix Functions
| From: | Christian Schneider | Date: | Fri, 27 Feb 2026 21:28:00 +0000 |
| Subject: | Re: [RFC] [VOTE] Prefix and Suffix Functions | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-130200@lists.php.net to get a copy of this message | ||
Am 27.02.2026 um 14:31 schrieb Barel <barel.barelon@gmail.com>:
> This is to announce the opening of the vote for the Prefix and Suffix Functions RFC
>
> RFC: https://wiki.php.net/rfc/prefix_suffix_functions
> Discussion thread:
> https://news-web.php.net/php.internals/129842
My reason to vote no was that
a) it is sort of a matrix of functions (prefix|suffix x ensure|remove|replace) a doing something
very similar
b) there are several downsides to these specific implementations
The downsides I see at a first glance:
- case sensitive: str_suffix_replace(".jpeg", ".jpg", $filename) does not work
with "Image.JPEG"
- *_ensure doesn't handle multiples: str_suffix_ensure($path, '/') does not handle
"/api//"
This leads me to the conclusion that I'd probably stick to the old-school use of preg_replace
which handles *all* the things these functions try to solve.
And while I agree that regular expressions are a bit harder to read they (to me) are simple enough
while not having these downsides.
E.g. I would rewrite
$file = str_suffix_replace('.jpeg', '.jpg', $file);
as
$file = preg_replace('#\.jpeg$#i', '.jpg', $file); # Case insentive, easily
extended also handle jpe and jfif
And instead of
$path = str_suffix_ensure($path, '/');
I would write
$path1 = preg_replace('#/*$#', '/', $path, 1); # Ensure one single slashes at
the end
or maybe even
$path = preg_replace('#/+#', '/', "$path/"); # Also normalizes all
multiple slashes in the path
Side-note: Regular expressions handle all the stuff described in the Future Scope section.
Some people like long and descriptive special functions for (depending on the context) common cases
but I realized that I often prefer being able to reuse powerful functionality instead of having to
guess if a specialized function might exists even with modern dev environments make finding the
right function easier. YMMV.
Regards,
- Chris