Skip to content

Fix substrReplace to be multibyte safe - #57936

Merged
taylorotwell merged 3 commits into
laravel:12.xfrom
Nasim25:fix-multibyte-substrreplace
Nov 27, 2025
Merged

taylorotwell merged 3 commits into
laravel:12.xfrom
Nasim25:fix-multibyte-substrreplace

Conversation

@Nasim25

@Nasim25 Nasim25 commented Nov 26, 2025

Copy link
Copy Markdown
Contributor

Description

Fixes Str::substrReplace() to properly handle multibyte UTF-8 characters.

Problem

Current implementation uses substr_replace() which operates on bytes, causing corruption with multibyte characters:

Str::substrReplace('kenkä', 'ng', -3, 2);
// Current: "kenng¤" ❌
// Expected: "kenga" ✅

Solution

  • Replaced substr_replace() with mb_substr() concatenation
  • Properly normalizes negative offsets and lengths
  • Maintains full backward compatibility

Testing

Str::substrReplace('kenkä', 'ng', -3, 2);  // 'kengä' ✅
Str::substrReplace('kenka', 'ng', -3, 2);  // 'kenga' ✅

Added tests for multibyte string handling, and tests pass.

@taylorotwell
taylorotwell merged commit 4b1344d into laravel:12.x Nov 27, 2025
1 check passed
akyrey pushed a commit to akyrey/framework that referenced this pull request Dec 29, 2025
* Fix substrReplace to be multibyte safe

* Apply code style fixes

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
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