Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -1747,11 +1747,19 @@ public static function substrCount($haystack, $needle, $offset = 0, $length = nu
*/
public static function substrReplace($string, $replace, $offset = 0, $length = null)
{
if ($length === null) {
$length = strlen($string);
$encoding = 'UTF-8';
$strlen = mb_strlen($string, $encoding);

if ($offset < 0) {
$offset = $offset < -$strlen ? 0 : $strlen + $offset;
}

return substr_replace($string, $replace, $offset, $length);
$length = is_null($length) ? $strlen - $offset : max($length, 0);

$start = mb_substr($string, 0, $offset, $encoding);
$end = mb_substr($string, $offset + $length, $strlen - ($offset + $length), $encoding);

return $start.$replace.$end;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,8 @@ public function testSubstrReplace()
$this->assertSame('12:00', Str::substrReplace('1200', ':', 2, 0));
$this->assertSame('The Laravel Framework', Str::substrReplace('The Framework', 'Laravel ', 4, 0));
$this->assertSame('Laravel – The PHP Framework for Web Artisans', Str::substrReplace('Laravel Framework', '– The PHP Framework for Web Artisans', 8));
$this->assertSame('Laravel – PHP 框架的网络专家', Str::substrReplace('Laravel 的网络专家', '– PHP 框架', 8, 0));
$this->assertSame('Laravel – PHP Фреймворк для веб-мастеров', Str::substrReplace('Laravel для веб-мастеров', ' – PHP Фреймворк', 7, 0));
}

public function testTake()
Expand Down