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] Adds Str::remove and Str::of('foobar')->remove methods #36639

Merged
merged 5 commits into from
Mar 18, 2021
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
23 changes: 23 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,29 @@ public static function replaceLast($search, $replace, $subject)
return $subject;
}

/**
* Remove any occurrence of the given string in the subject.
*
* @param string|array<string> $search
* @param string $subject
* @param bool $caseSensitive
* @return string
*/
public static function remove($search, $subject, $caseSensitive = true)
{
$regexSafeSearches = array_map(function ($search) {
return preg_quote($search);
}, is_array($search) ? $search : [$search]);

$regex = "/".implode("|", $regexSafeSearches)."/";

if (! $caseSensitive) {
$regex .= "i";
}

return preg_replace($regex, "", $subject);
}

/**
* Begin a string with a single instance of a given value.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,18 @@ public function prepend(...$values)
return new static(implode('', $values).$this->value);
}

/**
* Remove any occurrence of the given string in the subject.
*
* @param string|array<string> $search
* @param bool $caseSensitive
* @return static
*/
public function remove($search, $caseSensitive = true)
{
return new static(Str::remove($search, $this->value, $caseSensitive));
}

/**
* Replace the given value in the given string.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,20 @@ public function testReplaceLast()
$this->assertSame('Malmö Jönköping', Str::replaceLast('', 'yyy', 'Malmö Jönköping'));
}

public function testRemove()
{
$this->assertSame("Fbar", Str::remove('o', 'Foobar'));
$this->assertSame("Foo", Str::remove('bar', 'Foobar'));
$this->assertSame("oobar", Str::remove('F', 'Foobar'));
$this->assertSame("Foobar", Str::remove('f', 'Foobar'));
$this->assertSame("oobar", Str::remove('f', 'Foobar', false));

$this->assertSame("Fbr", Str::remove(["o", "a"], 'Foobar'));
$this->assertSame("Fooar", Str::remove(["f", "b"], 'Foobar'));
$this->assertSame("ooar", Str::remove(["f", "b"], 'Foobar', false));
$this->assertSame("Foobar", Str::remove(["f", "|"], 'Foo|bar'));
}

public function testSnake()
{
$this->assertSame('laravel_p_h_p_framework', Str::snake('LaravelPHPFramework'));
Expand Down
15 changes: 15 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Support;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -452,6 +453,20 @@ public function testReplaceLast()
$this->assertSame('Malmö Jönköping', (string) $this->stringable('Malmö Jönköping')->replaceLast('', 'yyy'));
}

public function testRemove()
{
$this->assertSame("Fbar", (string) $this->stringable('Foobar')->remove('o'));
$this->assertSame("Foo", (string) $this->stringable('Foobar')->remove('bar'));
$this->assertSame("oobar", (string) $this->stringable('Foobar')->remove('F'));
$this->assertSame("Foobar", (string) $this->stringable('Foobar')->remove('f'));
$this->assertSame("oobar", (string) $this->stringable('Foobar')->remove('f', false));

$this->assertSame("Fbr", (string) $this->stringable('Foobar')->remove(["o", "a"]));
$this->assertSame("Fooar", (string) $this->stringable('Foobar')->remove(["f", "b"]));
$this->assertSame("ooar", (string) $this->stringable('Foobar')->remove(["f", "b"], false));
$this->assertSame("Foobar", (string) $this->stringable('Foo|bar')->remove(["f", "|"]));
}

public function testSnake()
{
$this->assertSame('laravel_p_h_p_framework', (string) $this->stringable('LaravelPHPFramework')->snake());
Expand Down