diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 5799c63f097f..6c377897dcf7 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -875,6 +875,24 @@ public static function substrCount($haystack, $needle, $offset = 0, $length = nu } } + /** + * Replace text within a portion of a string. + * + * @param string|array $string + * @param string|array $replace + * @param array|int $offset + * @param array|int|null $length + * @return string|array + */ + public static function substrReplace($string, $replace, $offset = 0, $length = null) + { + if ($length === null) { + $length = strlen($string); + } + + return substr_replace($string, $replace, $offset, $length); + } + /** * Make a string's first character uppercase. * diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 1927112c60b2..f22727578a68 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -709,6 +709,19 @@ public function substrCount($needle, $offset = null, $length = null) return Str::substrCount($this->value, $needle, $offset ?? 0, $length); } + /** + * Replace text within a portion of a string. + * + * @param string|array $replace + * @param array|int $offset + * @param array|int|null $length + * @return string|array + */ + public function substrReplace($replace, $offset = 0, $length = null) + { + return new static(Str::substrReplace($this->value, $replace, $offset, $length)); + } + /** * Trim the string of the given characters. * diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index bbdebd907637..de2e60349d00 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -535,6 +535,13 @@ public function testSubstrCount() $this->assertSame(1, Str::substrCount('laravelPHPFramework', 'a', -10, -3)); } + 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)); + } + public function testUcfirst() { $this->assertSame('Laravel', Str::ucfirst('laravel')); diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 8377b6f85453..3d2afc435b71 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -621,6 +621,13 @@ public function testSubstrCount() $this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('a', -10, -3)); } + public function testSubstrReplace() + { + $this->assertSame('12:00', (string) $this->stringable('1200')->substrReplace(':', 2, 0)); + $this->assertSame('The Laravel Framework', (string) $this->stringable('The Framework')->substrReplace('Laravel ', 4, 0)); + $this->assertSame('Laravel – The PHP Framework for Web Artisans', (string) $this->stringable('Laravel Framework')->substrReplace('– The PHP Framework for Web Artisans', 8)); + } + public function testPadBoth() { $this->assertSame('__Alien___', (string) $this->stringable('Alien')->padBoth(10, '_'));