Skip to content

Commit c8d7bbd

Browse files
committed
Merge branch 'str-remove' into 8.x
2 parents ffd719e + 20e2470 commit c8d7bbd

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

src/Illuminate/Support/Str.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Support;
44

5+
use Illuminate\Support\Arr;
56
use Illuminate\Support\Traits\Macroable;
67
use League\CommonMark\GithubFlavoredMarkdownConverter;
78
use Ramsey\Uuid\Codec\TimestampFirstCombCodec;
@@ -558,6 +559,25 @@ public static function replaceLast($search, $replace, $subject)
558559
return $subject;
559560
}
560561

562+
/**
563+
* Remove any occurrence of the given string in the subject.
564+
*
565+
* @param string|array<string> $search
566+
* @param string $subject
567+
* @param bool $caseSensitive
568+
* @return string
569+
*/
570+
public static function remove($search, $subject, $caseSensitive = true)
571+
{
572+
foreach (Arr::wrap($search) as $s) {
573+
$subject = $caseSensitive
574+
? str_replace($search, '', $subject)
575+
: str_ireplace($search, '', $subject);
576+
}
577+
578+
return $subject;
579+
}
580+
561581
/**
562582
* Begin a string with a single instance of a given value.
563583
*

src/Illuminate/Support/Stringable.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,18 @@ public function prepend(...$values)
467467
return new static(implode('', $values).$this->value);
468468
}
469469

470+
/**
471+
* Remove any occurrence of the given string in the subject.
472+
*
473+
* @param string|array<string> $search
474+
* @param bool $caseSensitive
475+
* @return static
476+
*/
477+
public function remove($search, $caseSensitive = true)
478+
{
479+
return new static(Str::remove($search, $this->value, $caseSensitive));
480+
}
481+
470482
/**
471483
* Replace the given value in the given string.
472484
*

tests/Support/SupportStrTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,20 @@ public function testReplaceLast()
365365
$this->assertSame('Malmö Jönköping', Str::replaceLast('', 'yyy', 'Malmö Jönköping'));
366366
}
367367

368+
public function testRemove()
369+
{
370+
$this->assertSame("Fbar", Str::remove('o', 'Foobar'));
371+
$this->assertSame("Foo", Str::remove('bar', 'Foobar'));
372+
$this->assertSame("oobar", Str::remove('F', 'Foobar'));
373+
$this->assertSame("Foobar", Str::remove('f', 'Foobar'));
374+
$this->assertSame("oobar", Str::remove('f', 'Foobar', false));
375+
376+
$this->assertSame("Fbr", Str::remove(["o", "a"], 'Foobar'));
377+
$this->assertSame("Fooar", Str::remove(["f", "b"], 'Foobar'));
378+
$this->assertSame("ooar", Str::remove(["f", "b"], 'Foobar', false));
379+
$this->assertSame("Foobar", Str::remove(["f", "|"], 'Foo|bar'));
380+
}
381+
368382
public function testSnake()
369383
{
370384
$this->assertSame('laravel_p_h_p_framework', Str::snake('LaravelPHPFramework'));

tests/Support/SupportStringableTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Tests\Support;
44

55
use Illuminate\Support\Collection;
6+
use Illuminate\Support\Str;
67
use Illuminate\Support\Stringable;
78
use PHPUnit\Framework\TestCase;
89

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

456+
public function testRemove()
457+
{
458+
$this->assertSame("Fbar", (string) $this->stringable('Foobar')->remove('o'));
459+
$this->assertSame("Foo", (string) $this->stringable('Foobar')->remove('bar'));
460+
$this->assertSame("oobar", (string) $this->stringable('Foobar')->remove('F'));
461+
$this->assertSame("Foobar", (string) $this->stringable('Foobar')->remove('f'));
462+
$this->assertSame("oobar", (string) $this->stringable('Foobar')->remove('f', false));
463+
464+
$this->assertSame("Fbr", (string) $this->stringable('Foobar')->remove(["o", "a"]));
465+
$this->assertSame("Fooar", (string) $this->stringable('Foobar')->remove(["f", "b"]));
466+
$this->assertSame("ooar", (string) $this->stringable('Foobar')->remove(["f", "b"], false));
467+
$this->assertSame("Foobar", (string) $this->stringable('Foo|bar')->remove(["f", "|"]));
468+
}
469+
455470
public function testSnake()
456471
{
457472
$this->assertSame('laravel_p_h_p_framework', (string) $this->stringable('LaravelPHPFramework')->snake());

0 commit comments

Comments
 (0)