[RFC] Prepared Literal String Matching and Replacement

From: Date: Fri, 06 Nov 2026 12:59:32 +0000
Subject: [RFC] Prepared Literal String Matching and Replacement
Groups: php.internals 
Request: Send a blank email to internals+get-133028@lists.php.net to get a copy of this message
Hi internals, I'd like to open discussion on this RFC: https://wiki.php.net/rfc/prepared_literal_string_matching It proposes Str\Matcher and Str\Replacer for prepared literal substring search and replacement. Matcher provides presence checks, unique matched keywords, occurrence counts, and enumeration of all overlapping matches with byte offsets, using Str\MatchResult objects. Replacer performs non-overlapping replacements, choosing the earliest start and then the longest key, without rescanning inserted text. Both classes prepare an immutable dictionary during construction. Matching is byte-oriented, binary-safe, and case-sensitive. Objects can be reused within a request or in long-running applications. I've implemented the API and published reproducible benchmarks covering both prepared calls and one-shot use, including construction and destruction. Preparation can pay off even for a single call on some inputs. On others, existing PHP functions or PCRE remain faster, even than a prepared object. I propose including these classes in ext/standard in the Str namespace. The current implementation is in ext/strmatch. I'd welcome feedback on the API, integration, error types, and whether Matcher and Replacer should be considered together or separately. Implementation: https://github.com/php/php-src/pull/25122 Benchmarks, methodology and raw data: https://github.com/ArtUkrainskiy/php-src-bench/tree/main/reports/multi-pattern-search -- Best regards, Artem Ukrainskiy

« previous php.internals (#133028) next »