Repository navigation
[4.x] Fix StringHelper::str_ireplace() for characters whose lowercase form has a different length - #73
Open
Hackwar wants to merge 1 commit into
Open
[4.x] Fix StringHelper::str_ireplace() for characters whose lowercase form has a different length#73Hackwar wants to merge 1 commit into
Hackwar wants to merge 1 commit into
Conversation
…ength str_ireplace() searched the lowercased string but applied the byte offsets to the original one, and replaced the byte length of the search string. Characters whose lowercase form has a different byte length (Ⱥ, ẞ) shifted the offsets, so the wrong bytes were replaced and the result could be invalid UTF-8. Match case-insensitively on the original string with a /iu regular expression instead, and insert the replacement literally. Document that $count is a limit, not a by-reference count as in the native function. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
|
The PHPStan job fails with |
This was referenced Nov 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of Changes
StringHelper::str_ireplace()searchedmb_strtolower($str)but applied the byte offsets it found to the original$str, and always replacedstrlen($search)bytes. When lowercasing changes the byte length of a character, the offsets drift and the wrong bytes are replaced:The method now matches case-insensitively on the original string with
preg_replace_callback('/…/iu'), so offsets and match lengths always refer to the string that is modified. The replacement is inserted literally ($0or\1in$replaceare not expanded).The fourth parameter keeps its current meaning, a maximum number of replacements per search string. The docblock described it as a by-reference count like in the native
str_ireplace(), which it never was, so the docblock now says it is a limit and thatnull,trueandfalsemean no limit. A separate by-reference count would be an API change for 5.0 and is not part of this PR.Testing Instructions
New cases in
StringHelperTest::seedTestStr_ireplace:Ⱥ(lowercase is longer),ẞ(lowercase is shorter),ẞmatched byß, regex characters in the search string,$0in the replacement, and the limit (1 and 0). The first three fail before the change and pass after it. The existing cases still pass,vendor/bin/phpunitis green, phpcs is clean, and PHPStan reports no errors.Documentation Changes Required
docs/overview.md(still "TODO" on4.x-dev) gets aStringHelper::str_ireplace()section that describes the case-insensitive matching and the$countlimit.The owner's docs PR #72 rewrites
docs/overview.mdand describes the current bug. Whichever of the two is merged second has to fold this section into #72's structure and drop thestr_ireplace()paragraph under "Notes on individual methods" (the drifting offsets and the advice to usepreg_replace('/…/iu', …)instead).🤖 Generated with Claude Code