Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

strip_tags

Removes HTML tags from a string.

Syntax

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

Parameters

ParameterTypeDescription
$stringstringThe source string.
$allowed_tagsarray|string|nullOptional. The tags to keep.

Return value

stringThe string without tags.

Examples

Text only:

<?php
 
echo strip_tags('<p>Hello, <b>world</b>!</p>');

Output:

Hello, world!

Allowed tags stay:

<?php
 
echo strip_tags('<p>Hello, <b>world</b>!</p>', ['b']);

Output:

Hello, <b>world</b>!

Notes

The function does not protect against XSS: the attributes of allowed tags stay in place. Use htmlspecialchars() to print user text.

See also

  • htmlspecialchars — Replaces HTML special characters with safe entities.
  • trim — Removes whitespace and other characters from both ends of a string.

Related recipes

In the tutorial

  • Security — The basics of PHP site security — XSS and escaping, SQL injection, password hashing, CSRF tokens and validating input.

Thanks for reading!

Donate to the site — we work hard for you.