Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Improvements to the Stringable::replace() method #37186

Merged
merged 14 commits into from
Apr 30, 2021
13 changes: 13 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,19 @@ public static function replaceArray($search, array $replace, $subject)
return $result;
}

/**
* Replace the given value in the given string.
*
* @param string|string[] $search
* @param string|string[] $replace
* @param string|string[] $subject
* @return static
*/
public static function replace($search, $replace, $subject)
{
return str_replace($search, $replace, $subject);
}

/**
* Replace the first occurrence of a given value in the string.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public function repeat(int $times)
*/
public function replace($search, $replace)
{
return new static(str_replace($search, $replace, $this->value));
return new static(Str::replace($search, $replace, $this->value));
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,14 @@ public function testRandom()
$this->assertIsString(Str::random());
}

public function testReplace()
{
$this->assertSame('foo bar laravel', Str::replace('baz', 'laravel', 'foo bar baz'));
$this->assertSame('foo bar baz 8.x', Str::replace('?', '8.x', 'foo bar baz ?'));
$this->assertSame('foo/bar/baz', Str::replace(' ', '/', 'foo bar baz'));
$this->assertSame('foo bar baz', Str::replace(['?1', '?2', '?3'], ['foo', 'bar', 'baz'], '?1 ?2 ?3'));
}

public function testReplaceArray()
{
$this->assertSame('foo/bar/baz', Str::replaceArray('?', ['foo', 'bar', 'baz'], '?/?/?'));
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@ public function testLength()
$this->assertSame(11, $this->stringable('foo bar baz')->length('UTF-8'));
}

public function testReplace()
{
$this->assertSame('foo/foo/foo', (string) $this->stringable('?/?/?')->replace('?', 'foo'));
$this->assertSame('bar/bar', (string) $this->stringable('?/?')->replace('?', 'bar'));
$this->assertSame('?/?/?', (string) $this->stringable('? ? ?')->replace(' ', '/'));
$this->assertSame('foo/bar/baz/bam', (string) $this->stringable('?1/?2/?3/?4')->replace(['?1', '?2', '?3', '?4'], ['foo','bar','baz', 'bam']));
}

public function testReplaceArray()
{
$this->assertSame('foo/bar/baz', (string) $this->stringable('?/?/?')->replaceArray('?', ['foo', 'bar', 'baz']));
Expand Down