From 617098d1eb89e0b3a77d204bd1ef149829b4ead4 Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Fri, 13 Nov 2026 01:03:43 +0100 Subject: [PATCH] Fix StringHelper::str_ireplace() for case mappings that change byte length MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- Tests/StringHelperTest.php | 7 +++++++ docs/overview.md | 18 ++++++++++++++++++ src/StringHelper.php | 36 ++++++++++++++++-------------------- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/Tests/StringHelperTest.php b/Tests/StringHelperTest.php index 8d6fd578..2b577872 100644 --- a/Tests/StringHelperTest.php +++ b/Tests/StringHelperTest.php @@ -157,6 +157,13 @@ public static function seedTestStr_ireplace(): array [['PIG', 'JUMPED'], ['cow', 'hopped'], 'the pig jumped over the pig', true, 'the cow hopped over the cow'], ['шил', 'биш', 'Би шил идэй чадна', true, 'Би биш идэй чадна'], ['/', ':', '/test/slashes/', true, ':test:slashes:'], + 'lowercase is longer than the original' => ['x', 'y', 'Ⱥx', null, 'Ⱥy'], + 'lowercase is shorter than the original' => ['ab', 'Z', 'ẞAB', null, 'ẞZ'], + 'case variant of different byte length' => ['ß', 'ss', 'STRAẞE', null, 'STRAssE'], + 'regular expression characters in search' => ['.*', '!', 'a.*b.*', null, 'a!b!'], + 'replacement is taken literally' => ['b', '$0', 'abc', null, 'a$0c'], + 'count limits the replacements' => ['b', 'c', 'abab', 1, 'acab'], + 'count of zero replaces nothing' => ['b', 'c', 'abab', 0, 'abab'], ]; } diff --git a/docs/overview.md b/docs/overview.md index 5c220497..eac25665 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -1,3 +1,21 @@ # String package TODO + +## `StringHelper::str_ireplace()` + +`StringHelper::str_ireplace($search, $replace, $str, $count = null)` replaces every case-insensitive +occurrence of `$search` in `$str`. It matches on the original string with Unicode case folding, so +characters whose lowercase form has a different byte length (`Ⱥ`, `ẞ`) are handled correctly, and the +replacement is inserted literally. + +Unlike PHP's `str_ireplace()`, the fourth argument is **not** a by-reference variable that receives +the number of replacements. It is a limit: at most `$count` occurrences of each search string are +replaced. `null`, `true` and `false` mean no limit. + +```php +use Joomla\String\StringHelper; + +StringHelper::str_ireplace('b', 'c', 'abAB'); // 'acAc' +StringHelper::str_ireplace('b', 'c', 'abAB', 1); // 'acAB' +``` diff --git a/src/StringHelper.php b/src/StringHelper.php index 98f73e7d..605de582 100644 --- a/src/StringHelper.php +++ b/src/StringHelper.php @@ -262,7 +262,9 @@ public static function strlen($str) * @param string|string[] $search String to search * @param string|string[] $replace Existing string to replace * @param string $str New string to replace with - * @param integer|null|boolean $count Optional count value to be passed by reference + * @param integer|null|boolean $count Optional maximum number of replacements per search string. Unlike the native + * str_ireplace(), this is a limit, not a by-reference variable that receives + * the number of replacements. Null, true or false mean no limit. * * @return string UTF-8 String * @@ -272,29 +274,23 @@ public static function strlen($str) public static function str_ireplace($search, $replace, $str, $count = null) { if (!is_array($search)) { - $slen = strlen($search); - if ($slen == 0) { + $search = (string) $search; + + if ($search === '') { return $str; } - $lendif = strlen($replace) - strlen($search); - $search = mb_strtolower($search); + // Match on the original string, so offsets and lengths are not affected by case mapping + $result = preg_replace_callback( + '/' . preg_quote($search, '/') . '/iu', + static function () use ($replace) { + return (string) $replace; + }, + $str, + \is_int($count) ? $count : -1 + ); - $search = preg_quote($search, '/'); - $lstr = mb_strtolower($str); - $i = 0; - $matched = 0; - while (preg_match('/(.*)' . $search . '/Us', $lstr, $matches)) { - if ($i === $count) { - break; - } - $mlen = strlen($matches[0]); - $lstr = substr($lstr, $mlen); - $str = substr_replace($str, $replace, $matched + strlen($matches[1]), $slen); - $matched += $mlen + $lendif; - $i++; - } - return $str; + return $result ?? $str; } else { foreach (array_keys($search) as $k) { if (is_array($replace)) {