Skip to content

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

@ighea

Description

@ighea

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ä"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions