Documentation

StripTags — Zend Framework 2 2.1.5 documentation

StripTags

This filter can strip XML and HTML tags from given content.

Warning

Be warned that Zend\Filter\StripTags should only be used to strip all available tags.

Using Zend\Filter\StripTags to make your site secure by stripping some unwanted tags will lead to unsecure and dangerous code.

Zend\Filter\StripTags must not be used to prevent XSS attacks. This filter is no replacement for using Tidy or HtmlPurifier.

Supported Options

The following options are supported for Zend\Filter\StripTags:

  • allowAttribs: This option sets the attributes which are accepted. All other attributes are

stripped from the given content.

  • allowTags: This option sets the tags which are accepted. All other tags will be stripped

from the given content.

Basic Usage

See the following example for the default behaviour of this filter:

1
2
3
$filter = new Zend\Filter\StripTags();

print $filter->filter('<B>My content</B>');

As result you will get the stripped content ‘My content’.

When the content contains broken or partial tags then the complete following content will be erased. See the following example:

1
2
3
$filter = new Zend\Filter\StripTags();

print $filter->filter('This contains <a href="http://example.com">no ending tag');

The above will return ‘This contains’ with the rest being stripped.

Allowing Defined Tags

Zend\Filter\StripTags allows stripping of all but defined tags. This can be used for example to strip all tags but links from a text.

1
2
3
4
$filter = new Zend\Filter\StripTags(array('allowTags' => 'a'));

$input  = "A text with <br/> a <a href='link.com'>link</a>";
print $filter->filter($input);

The above will return ‘A text with a <a href=’link.com’>link</a>’ as result. It strips all tags but the link. By providing an array you can set multiple tags at once.

Warning

Do not use this feature to get a probably secure content. This component does not replace the use of a proper configured html filter.

Allowing Defined Attributes

It is also possible to strip all but allowed attributes from a tag.

1
2
3
4
$filter = new Zend\Filter\StripTags(array('allowAttribs' => 'src'));

$input  = "A text with <br/> a <img src='picture.com' width='100'>picture</img>";
print $filter->filter($input);

The above will return ‘A text with a <img src=’picture.com’>picture</img>’ as result. It strips all tags but img. Additionally from the img tag all attributes but src will be stripped. By providing an array you can set multiple attributes at once.

Copyright

© 2006-2019 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts