Skip to content

[4.x] Fix StringHelper::str_ireplace() for characters whose lowercase form has a different length - #73

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-str-ireplace-offsets
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-str-ireplace-offsets

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

StringHelper::str_ireplace() searched mb_strtolower($str) but applied the byte offsets it found to the original $str, and always replaced strlen($search) bytes. When lowercasing changes the byte length of a character, the offsets drift and the wrong bytes are replaced:

StringHelper::str_ireplace('x', 'y', 'Ⱥx');   // 'Ⱥxy' instead of 'Ⱥy'
StringHelper::str_ireplace('ab', 'Z', 'ẞAB'); // "\xE1ZB", invalid UTF-8

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 ($0 or \1 in $replace are 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 that null, true and false mean 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, $0 in 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/phpunit is green, phpcs is clean, and PHPStan reports no errors.

Documentation Changes Required

docs/overview.md (still "TODO" on 4.x-dev) gets a StringHelper::str_ireplace() section that describes the case-insensitive matching and the $count limit.

The owner's docs PR #72 rewrites docs/overview.md and describes the current bug. Whichever of the two is merged second has to fold this section into #72's structure and drop the str_ireplace() paragraph under "Notes on individual methods" (the drifting offsets and the advice to use preg_replace('/…/iu', …) instead).

🤖 Generated with Claude Code

…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>
@Hackwar

Hackwar commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

The PHPStan job fails with Function mb_trim/mb_ltrim/mb_rtrim/mb_ucfirst not found in code this PR does not touch. It fails the same way on 4.x-dev (scheduled run 40287592030), because PHPStan on PHP 8.3 does not see the PHP 8.4 functions from symfony/polyfill-mbstring. #66 fixes it by adding the polyfill to scanDirectories. All other jobs pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant