strip_tags
(PHP 4, PHP 5, PHP 7)
strip_tags — Strip HTML and PHP tags from a string
Description
$str
[, string $allowable_tags
] )
This function tries to return a string with all NULL bytes, HTML and PHP tags stripped
from a given str
. It uses the same tag stripping
state machine as the fgetss() function.
Parameters
-
str
-
The input string.
-
allowable_tags
-
You can use the optional second parameter to specify tags which should not be stripped.
Note:
HTML comments and PHP tags are also stripped. This is hardcoded and can not be changed with
allowable_tags
.Note:
In PHP 5.3.4 and later, self-closing XHTML tags are ignored and only non-self-closing tags should be used in
allowable_tags
. For example, to allow both <br> and <br/>, you should use:<?php
strip_tags($input, '<br>');
?>
Changelog
Version | Description |
---|---|
5.3.4 |
strip_tags() ignores self-closing XHTML
tags in
allowable_tags .
|
5.0.0 | strip_tags() is now binary safe. |
Examples
Example #1 strip_tags() example
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="https://www.gaudry.be/en/php-rf-#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";
// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>
The above example will output:
Test paragraph. Other text <p>Test paragraph.</p> <a href="#fragment">Other text</a>
Notes
Because strip_tags() does not actually validate the HTML, partial or broken tags can result in the removal of more text/data than expected.
This function does not modify any attributes on the tags that you allow
using allowable_tags
, including the
style and onmouseover attributes
that a mischievous user may abuse when posting text that will be shown
to other users.
Note:
Tag names within the input HTML that are greater than 1023 bytes in length will be treated as though they are invalid, regardless of the
allowable_tags
parameter.
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-strip-tags.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.