s3yfullah
s3yfullah

Reputation: 165

How to Remove Html Tags in PHP?

I use htmlspecialchars function in my string. But, I don't want to clean them;

<b>, <br>, <p> <ul>,<li> bla bla...

Example: Mystring = "<script>.....</script><br><b>test</b><p>aaaa</p>";

I want to; =

<script>.....</script>

Upvotes: 4

Views: 2409

Answers (4)

user846059
user846059

Reputation:

You can use built in PHP function: strip_tags($text, '<p><a><li><b>');

You don't need to use any class or external library, to remove HTML tags from user input.

In the above example I am cleaning the $text from all tags except <p><a><li><b>;

This PHP function will clean user input making sure are not submitting <script> or </div> to break your page.

Upvotes: 1

Can Aydoğan
Can Aydoğan

Reputation: 1040

You can use HTML Sanitizer Class - http://www.phpclasses.org/browse/package/3746.html

Upvotes: 2

Sagi
Sagi

Reputation: 8011

Have a look at HTML Purifier, and especially the whitelist feature.

This is probably the safest approach if you allow HTML tags. You can view the comparison here.

Upvotes: 7

Pekka
Pekka

Reputation: 449813

You want to remove all tags? Use strip_tags().

Upvotes: 3

Related Questions