PHP strip_tags() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strip_tags() removes HTML and PHP tags from a string.

You can pass a list of tags that should stay.

Syntax

Call strip_tags() with the values you need. Start with this line.

PHP
$text = strip_tags($html);
CallWhat it does
strip_tags("<p>Hello</p>")Returns "Hello".
strip_tags($html, "<b>")Removes all tags except <b>.

Since PHP 7.4 you can pass allowed tags as an array, such as ["b", "i"].

Library declarations
PHP
strip_tags(string $string, array|string|null $allowed_tags = null): string

Parameters

ParameterDescription
stringRequired. The string that may contain HTML tags.
allowed_tagsOptional. Tags to keep, as a string like "<b><i>" or an array like ["b", "i"].

Return Value

Return ValueDescription
stringThe string with tags removed.

PHP Examples

Example 1 — Remove All Tags

Keep only the visible text.

PHP
<?php
$html = "<p>Hello <b>World</b></p>";

echo strip_tags($html), PHP_EOL;

How It Works

  1. Start with HTML. It has p and b tags.
  2. Call strip_tags(). Every tag is removed.
  3. Text stays. Only the words remain.
  4. Print the result. It reads as plain text.

Example 2 — Keep Some Tags

List the tags you want to allow.

PHP
<?php
$html = "<p>Hello <b>World</b> <i>again</i></p>";

echo strip_tags($html, "<b>"), PHP_EOL;
echo strip_tags($html, ["b", "i"]), PHP_EOL;

How It Works

  1. Allow b with a string. "<b>" keeps bold tags.
  2. Other tags are removed. p and i are gone.
  3. Use an array. ["b", "i"] works in PHP 7.4+.
  4. Both tags stay. Only p is removed.

Example 3 — Script Text Is Not Removed

Tags are removed, but the text inside them is not.

PHP
<?php
$input = "<script>alert(1)</script>Hi";

echo strip_tags($input), PHP_EOL;
echo htmlspecialchars($input), PHP_EOL;

How It Works

  1. Pass a script tag. It has text inside.
  2. strip_tags() removes tags. The words alert(1) remain.
  3. Use htmlspecialchars(). It turns tags into safe text.
  4. Pick the right tool. Escape output instead of trying to strip it.

Frequently Asked Questions

strip_tags() removes HTML and PHP tags from a string and returns the remaining text.
strip_tags(string $string, array|string|null $allowed_tags = null): string. The second argument lists tags that should stay.
Pass them as a string like "<b><i>" or, in PHP 7.4 and later, as an array like ["b", "i"].
Not by itself. It leaves text inside tags and can be tricked by broken HTML. Use htmlspecialchars() when printing user input.
strip_tags() removes tags. htmlspecialchars() keeps them but turns them into harmless text, such as &lt;b&gt;.

Next: stripcslashes()

stripcslashes() turns C-style escape sequences back into real characters.

stripcslashes() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful