Skip to content

Commit 9b85771

Browse files
Add stringable support for strip_tags() (#39098)
* Update Stringable.php * Update SupportStringableTest.php * Update Stringable.php * Update SupportStringableTest.php * Update Stringable.php * Update SupportStringableTest.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent e1089e1 commit 9b85771

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/Illuminate/Support/Stringable.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,17 @@ public function start($prefix)
565565
return new static(Str::start($this->value, $prefix));
566566
}
567567

568+
/**
569+
* Strip HTML and PHP tags from the given string.
570+
*
571+
* @param string $allowedTags
572+
* @return static
573+
*/
574+
public function stripTags($allowedTags = null)
575+
{
576+
return new static(strip_tags($this->value, $allowedTags));
577+
}
578+
568579
/**
569580
* Convert the given string to upper-case.
570581
*

tests/Support/SupportStringableTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,4 +686,12 @@ public function testWordCount()
686686
$this->assertEquals(2, $this->stringable('Hello, world!')->wordCount());
687687
$this->assertEquals(10, $this->stringable('Hi, this is my first contribution to the Laravel framework.')->wordCount());
688688
}
689+
690+
public function testStripTags()
691+
{
692+
$this->assertSame('beforeafter', (string) $this->stringable('before<br>after')->stripTags());
693+
$this->assertSame('before<br>after', (string) $this->stringable('before<br>after')->stripTags('<br>'));
694+
$this->assertSame('before<br>after', (string) $this->stringable('<strong>before</strong><br>after')->stripTags('<br>'));
695+
$this->assertSame('<strong>before</strong><br>after', (string) $this->stringable('<strong>before</strong><br>after')->stripTags('<br><strong>'));
696+
}
689697
}

0 commit comments

Comments
 (0)