Re: [RFC] [Discussion] array_str_contains() for PHP 8.7
| From: | سپهر محمودی | Date: | Sun, 30 Aug 2026 22:20:31 +0000 |
| Subject: | Re: [RFC] [Discussion] array_str_contains() for PHP 8.7 | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-132392@lists.php.net to get a copy of this message | ||
در تاریخ دوشنبه ۳۱ اوت ۲۰۲۶، ۰۰:۵۸ Seifeddine Gmati
<azjezz@carthage.software> نوشت:
> On Sun, 30 Aug 2026 at 15:19, سپهر محمودی <sepehrphpr@gmail.com>
> wrote:
> >
> > Hi everyone,
> >
> > I'd like to start the discussion for a new RFC proposing the
>
array_str_contains() function for PHP 8.7.
>
> Hi Sepehr,
>
> > Filtering arrays based on substring matching is something many of us
> write on a regular basis, usually with boilerplate like:
> > $matches = array_filter($array, fn($item) => is_string($item) &&
> str_contains($item, $needle));
>
> I don't recall ever writing something like this. If I did, not
> remembering it suggests it isn't that common.
>
> The RFC also does not include any proof of the "regular basis", and
> under same conditions, the same case could be made for
> array_str_starts_with, array_str_ends_with, array_preg_match,
> array_str_length, and probably few more hunder combinations, I really
> don't see how str_contains is in any way special.
>
> The name array_str_contains is also confusing, it does not tell
> me
> what this function is doing, there is nothing indicating that it is
> filtering. array_str_contains($arr, $str) could mean that every
> string is joined with $str so by the end all string entries in
> $arr do contain $str? Idk.
>
> > Thanks,
> > Sepehr
>
> Cheers,
> Seifeddine.
>
--------
Hi Seifeddine,
Thank you for your feedback and perspective!
Regarding the use-case and frequency:
Sub-string filtering on lists of strings is a very common task across many
domains — such as autocomplete suggestions, filtering file/directory lists,
simple search filters over tag/category arrays, and processing logs or URL
lists.
While array_filter with a closure can achieve this, it introduces
noticeable overhead in userland due to repeated closure invocations and
type checks on every element. Implementing this natively in C provides
direct memory traversal and immediate performance gains for a very frequent
real-world operation.
Regarding other variants (starts_with, ends_with, etc.):
str_contains is arguably the most general and widely-used substring
operation. However, discussing whether a broader set of string-array
utilities or a more specific naming convention makes sense is exactly why
this RFC is in discussion.
Regarding the naming (array_str_contains):
You raise a fair point about clarity. Some developers might intuitively
expect a boolean return type (similar to in_array or str_contains) or
wonder if it acts as a filter. Alternative names like
array_filter_contains() or array_grep()-style semantics could also be
considered if the community prefers more explicit filtering terminology.
I appreciate your insights and look forward to hearing more thoughts from
the internals community on both the concept and the ideal naming.
Best regards,
Sepehr