Remove HTML tags from an Input with PHP

1

I would like to know if there is any way to check if, within a given text, there is the presence of html , css or script and, if there is, remove them.

I have a input on my site where users put text to be published. Even with a obs.just text , some users insert unwanted tags .

How can I get around this?

    
asked by anonymous 21.02.2017 / 01:23

1 answer

5

strip_tags

(PHP 4, PHP 5, PHP 7)

Remove the HTML and PHP tags from a string.

string strip_tags ( string $str [, string $allowable_tags ] )

This function tries to return a string by removing all HTML and PHP tags from str. Use the same system to remove the tags than fgetss () .

Parameters

  • str : The input string.
  • allowable_tags : You can use the second parameter, which is optional, to indicate tags that should not be removed.
  

Note : HTML comments and PHP tags are also removed. And this can not be modified with allowable_tags .

Return

Returns the modified string.

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text); // Test paragraph. Other text

See the example by running on Ideone .

    
21.02.2017 / 02:15