PHP strip_tags() Function

The strip_tags() function strips a string from HTML, XML, and PHP tags. It takes two parameters: the string to perform the action on and the specified html tags NOT to strip out. This is a useful function to prevent users from entering malicious scripts or unauthorized html onto your forms.


strip_tags ( $string, $allowable_tags );

$string – Required. The input string.
$allowable_tags – optional second parameter to specify tags which should not be stripped

* HTML comments and PHP tags are also stripped. This is hardcoded and can not be changed with allowable_tags.


$string = '<font>Learn Webdevelopment.</font><!–Comment text –> <a href="javascript:void(0);">Other text</a>';
echo strip_tags($string);
echo "<br>";

// Allow <p> and <a>
echo strip_tags($text, '<font><a>');

The output of the code above will be:

Learn Webdevelopment. Other text
<font> Learn Webdevelopment.</font> <a href="javascript:void(0);">Other text</a>

Posted in Php