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

[10.x] Add caseSensitive flag to Stringable replace function #46578

Merged
merged 2 commits into from
Mar 28, 2023
Merged
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
7 changes: 5 additions & 2 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -917,9 +917,10 @@ public static function replaceArray($search, $replace, $subject)
* @param string|iterable<string> $search
* @param string|iterable<string> $replace
* @param string|iterable<string> $subject
* @param bool $caseSensitive
* @return string
*/
public static function replace($search, $replace, $subject)
public static function replace($search, $replace, $subject, $caseSensitive = true)
{
if ($search instanceof Traversable) {
$search = collect($search)->all();
Expand All @@ -933,7 +934,9 @@ public static function replace($search, $replace, $subject)
$subject = collect($subject)->all();
}

return str_replace($search, $replace, $subject);
return $caseSensitive
? str_replace($search, $replace, $subject)
: str_ireplace($search, $replace, $subject);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,12 @@ public function repeat(int $times)
*
* @param string|iterable<string> $search
* @param string|iterable<string> $replace
* @param bool $caseSensitive
* @return static
*/
public function replace($search, $replace)
public function replace($search, $replace, $caseSensitive = true)
{
return new static(Str::replace($search, $replace, $this->value));
return new static(Str::replace($search, $replace, $this->value, $caseSensitive));
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,9 @@ public function testItCanSpecifyAFallbackForARandomStringSequence()
public function testReplace()
{
$this->assertSame('foo bar laravel', Str::replace('baz', 'laravel', 'foo bar baz'));
$this->assertSame('foo bar laravel', Str::replace('baz', 'laravel', 'foo bar Baz', false));
$this->assertSame('foo bar baz 8.x', Str::replace('?', '8.x', 'foo bar baz ?'));
$this->assertSame('foo bar baz 8.x', Str::replace('x', '8.x', 'foo bar baz X', false));
$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'));
$this->assertSame(['foo', 'bar', 'baz'], Str::replace(collect(['?1', '?2', '?3']), collect(['foo', 'bar', 'baz']), collect(['?1', '?2', '?3'])));
Expand Down
2 changes: 2 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,9 +825,11 @@ public function testLength()
public function testReplace()
{
$this->assertSame('foo/foo/foo', (string) $this->stringable('?/?/?')->replace('?', 'foo'));
$this->assertSame('foo/foo/foo', (string) $this->stringable('x/x/x')->replace('X', 'foo', false));
$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']));
$this->assertSame('?1/?2/?3/?4', (string) $this->stringable('foo/bar/baz/bam')->replace(['Foo', 'BaR', 'BAZ', 'bAm'], ['?1', '?2', '?3', '?4'], false));
$this->assertSame('foo/bar/baz/bam', (string) $this->stringable('?1/?2/?3/?4')->replace(collect(['?1', '?2', '?3', '?4']), collect(['foo', 'bar', 'baz', 'bam'])));
}

Expand Down