Re: [RFC] Deprecations for PHP 8.4
| From: | Tim Düsterhus | Date: | Thu, 27 Jun 2024 17:08:53 +0000 |
| Subject: | Re: [RFC] Deprecations for PHP 8.4 | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-123944@lists.php.net to get a copy of this message | ||
Hi
On 6/26/24 14:14, Gina P. Banyard wrote:
FWIW:Regarding explode($delimiter, $str)[0] — unless it is to be special-cased during compilation —it is a really inefficient way to find the substring up to the first character, especially for large strings and/or when in a tight loop where the explode is contained in a called functionThen use a regex: https://3v4l.org/SGWL5 Or a combination of strpos and substr. I'd bet that both of these solutions would use less memory, and I would guess the PCRE one should also be better for performance (although not benchmarked) as it is highly specialized in that task. There are *plenty* of solutions to the specific problem you pose here, and thus many different solutions more or less appropriate.
explode() also has a third parameter to limit the number of segments to return. If you always want just one, you can set the parameter to 2 to prevent further processing from happening after encountering the first delimiter.
Best regards
Tim Düsterhus