Categories: PHP

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.


jyoti rani

Recent Posts

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

6 months ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

6 months ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

6 months ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

6 months ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

6 months ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

6 months ago