Hi

On 6/26/24 14:14, Gina P. Banyard wrote:
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 function

Then 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.

FWIW: `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

Reply via email to