strip_tags – PHP String Functions

Syntax :

strip_tags ( string, allowable_tags);

Description :

strip_tags() function will Strip HTML and PHP tags from a string.

Note : HTML comments and PHP tags are also stripped. This is hardcoded and can not be changed with allowable_tags.
Note : This function is binary-safe.

Note : In PHP 5.3.4 and later versions, self-closing XHTML tags are ignored.  You should use only non-self-closing tags in allowable_tags. For example, to allow both <br> and <br/>, you should use:
<?php
strip_tags($input, ‘<br>’);
?>


Parameter :

  • string – This is a Required parameter. It is the input string.
  • allowable_tags – This is an Optional parameter. It specifies which tags are allowed in the input string. These tags will not be removed while stripping the tags by strip_tags() function.

Output :

It returns the stripped string with leftover tags which are in allowable_tags.


ChangeLog :

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, Description
PHP 5.3.4, This ignores self-closing XHTML tags in allowable_tags.
PHP 5.0 , The start and length parameters was added in this version.
PHP 4.3, HTML comments are always stripped.
[/table]


Related articles : htmlspecialchars().


strip_tags() – PHP Functions Example 1 : There is a difference in search string “t” and “T”.
<?php
echo "In below example only bold(b) and paragraph(p) tags are allowed.";
echo strip_tags("<p>Hi from <b><i>Tutorialmines</i></b>.</p>","<b><p>");
echo strip_tags("<!--Testing comments --><p>Comment tag is always stripped off.</p>","<p>");
echo strip_tags("Php tags are <b>always stripped off</b>.<?php echo 'hdfg jdfgj g';?>","<b>");
?>

Output of above code in the browser is as below:

In below example only bold(b) and paragraph(p) tags are allowed.Hi from Tutorialmines.

Comment tag is always stripped off.

Php tags are always stripped off.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *