strip_tags()

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

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

This function tries to return a string with all NULL bytes, HTML and PHP tags stripped from a given string. It uses the same tag stripping state machine as the fgetss() function.

Signature

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

Parameters

ParameterTypeWhat it takes
$stringstringThe input string.
$allowed_tagsarray|string|nullYou can use the optional second parameter to specify tags which should not be stripped. These are either given as string, or as of PHP 7.4.0, as array. Refer to the example below regarding the format of this parameter.

Return value

Returns the stripped string.

Example

A runnable snippet:

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>'; echo strip_tags($text); echo "\n"; // Allow <p> and <a> echo strip_tags($text, '<p><a>'); // as of PHP 7.4.0 the line above can be written as: // echo strip_tags($text, ['p', 'a']);

Output:

Test paragraph. Other text <p>Test paragraph.</p> <a href="#fragment">Other text</a>

Changelog

VersionChange
8.0.0allowed_tags is nullable now.
7.4.0The allowed_tags now alternatively accepts an array.

See also

htmlspecialchars()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page strip_tags() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.