Skip to content

12.x fix: use multibyte-safe functions in Str::afterLast()#58457

Merged
taylorotwell merged 1 commit intolaravel:12.xfrom
irabbi360:fix/str-afterlast-multibyte-support
Jan 22, 2026
Merged

12.x fix: use multibyte-safe functions in Str::afterLast()#58457
taylorotwell merged 1 commit intolaravel:12.xfrom
irabbi360:fix/str-afterlast-multibyte-support

Conversation

@irabbi360
Copy link
Contributor

Problem

The Str::afterLast() method used byte-based string functions which don't properly handle multibyte UTF-8 characters:

Function Used Issue
strrpos() Returns byte position, not character position
substr() Operates on bytes, not characters
strlen() Returns byte count, not character count

Solution

Replace with multibyte-safe equivalents already used elsewhere in the Str class:

// Before
$position = strrpos($subject, (string) $search);
return substr($subject, $position + strlen($search));

// After
$position = mb_strrpos($subject, $search);
return static::substr($subject, $position + static::length($search));

Files Changed

  • src/Illuminate/Support/Str.php - Fix implementation
  • tests/Support/SupportStrTest.php - Add multibyte test cases

Test Coverage

$this->assertSame('', Str::afterLast('café au café', 'café'));
$this->assertSame('', Str::afterLast('こんにちは世界こんにちは', 'こんにちは'));

@taylorotwell taylorotwell merged commit 2164ba7 into laravel:12.x Jan 22, 2026
70 of 72 checks passed
@irabbi360 irabbi360 deleted the fix/str-afterlast-multibyte-support branch February 1, 2026 19:19
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