Remove HTML tags from an Input with PHP

Asked

Viewed 4,625 times

1

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

I have on my website a input where the user places a text to be published. Even with a "Obs.text only", some users insert tags unwanted.

How I can get around that?

  • Taking advantage of the moment, read the guide on how to ask, especially the part about how to create a minimal, complete and verifiable example. Following the guides will surely increase the quality of the content in the community.

1 answer

5

strip_tags

(PHP 4, PHP 5, PHP 7)

Removes 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. It uses the same system to remove tags from that fgetss().

Parameters

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

Note: HTML comments and PHP tags are also removed. And this cannot 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 running on Ideone.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.