PHP Function Reference

PHP strip_tags() Function



The PHP strip_tags() function attempts 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.

Syntax

strip_tags(string, allowed_tags)

Parameters

string Required. Specify the input string.
allowed_tags Optional. Specify tags which should not be stripped. These can be provided as string, or as of PHP 7.4.0, as array.

Note: HTML comments and PHP tags are always stripped. This can not be changed with allowed_tags. This function ignores self-closing XHTML tags and only non-self-closing tags should be used in allowed_tags.

Return Value

Returns the stripped string.

Example:

In the example below, strip_tags() function returns the right side trimmed version of the given string.

<?php
$text = 
'<p>Test paragraph.</p><!-- Comments --> 
<a href="#testlink">Some link</a>';

echo strip_tags($text);
echo "\n\n";

//allowing <p> and <a> tags
echo strip_tags($text, '<p><a>');
echo "\n\n";

//as of PHP 7.4.0 the above code can be written as
echo strip_tags($text, ['p', 'a']);
?>

The output of the above code will be:

Test paragraph. 
Some link

<p>Test paragraph.</p>
<a href="#testlink">Some link</a>

<p>Test paragraph.</p> 
<a href="#testlink">Some link</a>

❮ PHP String Reference