framework icon indicating copy to clipboard operation
framework copied to clipboard

Illuminate\Support\Str::substrReplace() is not multibyte safe

Open ighea opened this issue 3 weeks ago • 0 comments

Laravel Version

12.21.0

PHP Version

8.4.1

Database Driver & Version

No response

Description

Use of Illuminate\Support\Str::substrReplace() does not seem to be multibyte aware.

Source line seems to use non-multibyte safe PHP method substr_replace(): https://github.com/laravel/framework/blob/12.x/src/Illuminate/Support/Str.php#L1748

I don't argue that Str would mention somewhere being multibyte safe, but would it make sense to at least mention which methods are not or either rewrite this to be safe like majority of the methods seems to be?

Steps To Reproduce

Using thinker:

> str('kenka')->substrReplace('ng', -3, 2)
= Illuminate\Support\Stringable {#6231
    value: "kenga",
}

> str('kenkä')->substrReplace('ng', -3, 2)
= Illuminate\Support\Stringable {#6233
    value: b"kenng¤",
}

Expected:

str('kenkä')->substrReplace('ng', -3, 2)->value;
// Result: "kengä"

ighea avatar Nov 20 '25 06:11 ighea