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] Add stringable support for strip_tags() #39098

Merged
merged 6 commits into from
Oct 5, 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
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,17 @@ public function start($prefix)
return new static(Str::start($this->value, $prefix));
}

/**
* Strip HTML and PHP tags from the given string.
*
* @param string $allowedTags
* @return static
*/
public function stripTags($allowedTags = null)
{
return new static(strip_tags($this->value, $allowedTags));
}

/**
* Convert the given string to upper-case.
*
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,4 +686,12 @@ public function testWordCount()
$this->assertEquals(2, $this->stringable('Hello, world!')->wordCount());
$this->assertEquals(10, $this->stringable('Hi, this is my first contribution to the Laravel framework.')->wordCount());
}

public function testStripTags()
{
$this->assertSame('beforeafter', (string) $this->stringable('before<br>after')->stripTags());
$this->assertSame('before<br>after', (string) $this->stringable('before<br>after')->stripTags('<br>'));
$this->assertSame('before<br>after', (string) $this->stringable('<strong>before</strong><br>after')->stripTags('<br>'));
$this->assertSame('<strong>before</strong><br>after', (string) $this->stringable('<strong>before</strong><br>after')->stripTags('<br><strong>'));
}
}