[GIT-PULLS] [php-src] PR #23512: Add array_str_contains() function to standard library
[email protected] (sepehrphpr)
| Newsgroups | php.git-pulls |
|---|---|
| Message-ID | <[email protected]> |
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