[php-src] PR #23512: Add array_str_contains() function to standard library
| From: | sepehrphpr | Date: | Sun, 30 Aug 2026 13:43:14 +0000 |
| Subject: | [php-src] PR #23512: Add array_str_contains() function to standard library | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-38212@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/23512
Author: sepehrphpr
### Summary
This PR introduces the
array_str_contains(array $haystack, string $needle): array
function to the standard library for PHP 8.7.
### Motivation
Filtering arrays based on substring matching is a very common task in PHP applications. Currently,
developers often use:
```php
array_filter($array, fn($item) => is_string($item) && str_contains($item, $needle));
Providing a dedicated internal implementation in C brings significant performance improvements,
reduces userland boilerplate, and offers an intuitive API consistent with str_contains().
Behavior & Details
Filtering & Key Preservation: Returns an array containing only the elements that contain
$needle, preserving their original keys (both numeric and string keys).
Type Handling: Safely ignores non-string values within the array without throwing
notices/warnings.
Empty Needle: If $needle is an empty string "", it matches all string elements
(consistent with str_contains()).
Binary & UTF-8 Safe: Built on top of php_memnstr(), ensuring fast and safe matching for
UTF-8 / multi-byte characters and binary strings.
Memory & Refcount Safe: Properly handles zval dereferencing and reference counting without
leaks.
Tests
Added comprehensive test suite covering basic matching, numeric/string key preservation, non-string
filtering, empty needle, and UTF-8 multi-byte strings in
ext/standard/tests/array/array_str_contains..phpt