Skip to content

[13.x] Fix multibyte case-insensitive matching in Str::replace and Str::remove#60882

Open
marekmiklusek wants to merge 2 commits into
laravel:13.xfrom
marekmiklusek:fix/str-replace-case-insensitive-multibyte
Open

[13.x] Fix multibyte case-insensitive matching in Str::replace and Str::remove#60882
marekmiklusek wants to merge 2 commits into
laravel:13.xfrom
marekmiklusek:fix/str-replace-case-insensitive-multibyte

Conversation

@marekmiklusek

Copy link
Copy Markdown
Contributor

Str::replace() and Str::remove() with $caseSensitive = false delegate
to str_ireplace(), which only case-folds ASCII characters. Multibyte
characters are never matched case-insensitively:

Str::replace('ž', 'X', 'Žltý kôň', false);     // "Žltý kôň" — nothing replaced
Str::replace('KÔŇ', 'pes', 'žltý kôň', false); // "žltý kôň" — nothing replaced
Str::remove('ž', 'Žltý', false);               // "Žltý" — nothing removed

This is inconsistent within the class itself -> Str::contains() considers
the same strings a case-insensitive match:

Str::contains('Žltý', 'ž', ignoreCase: true); // true

ASCII-only search terms keep using str_ireplace() exactly as before.
Multibyte terms are matched via preg_quote + /iu patterns with a callback
replacement, replicating str_ireplace() array pairing and sequential
semantics. Subjects that are not valid UTF-8 fall back to str_ireplace(),
preserving the previous behavior.

Comment thread src/Illuminate/Support/Str.php Outdated
Comment on lines +1295 to +1302
* @param string|iterable<string> $search
* @param string|iterable<string> $replace
* @param string|iterable<string> $subject
* @return string|string[]
*/
protected static function replaceIgnoreCase($search, $replace, $subject)
{
$searches = is_array($search) ? array_values($search) : [$search];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure but this probably breaks on non-array iterables, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on the docblock! The method itself can only receive normalized
input, both public entry points (replace() and remove()) convert
Traversable searches via iterator_to_array() before calling it, and the
existing collect() test in testReplace covers that path. But the
iterable<string> docblock on the helper was inaccurate for what it
actually accepts, updated it to array|string. Thanks Man!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're welcome!

Comment on lines +1295 to +1297
* @param array|string $search
* @param array|string $replace
* @param array|string $subject

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably narrow this down to

Suggested change
* @param array|string $search
* @param array|string $replace
* @param array|string $subject
* @param string|string[] $search
* @param string|string[] $replace
* @param string|string[] $subject

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.

2 participants