StripTags

StripTags - Strip HTML and PHP tags from a string.

StripTags only works with string, any other type is ignored.

Basic usage

use Jawira\Sanitizer\Filters as Sanitizer;

class Article {
  #[Sanitizer\StripTags]
  public string $content;
}

Parameters

type array (optional):
Array of allowed tags.
Default value is empty array.

Examples

Strip all html tags.

use Jawira\Sanitizer\Filters as Sanitizer;

class Article {
  #[Sanitizer\StripTags]
  public string $content;
}
"Foo<br>Bar" → "FooBar"
"<p>Hello <strong>John</strong></p>" → "Hello John"
"Foo <!-- comment --> Bar" → "Foo  Bar"

Strip all html tags but <br> and <p> tags.

use Jawira\Sanitizer\Filters as Sanitizer;

class Article {
  #[Sanitizer\StripTags(allowedTags: ['br', 'p'])]
  public string $content;
}
"Foo<br>Bar" → "Foo<br>Bar"
"<p>Hello <strong>John</strong></p>" → "<p>Hello John</p>"

See also

Replace - Replace all occurrences of the search string with the replacement string.