2

I would like to delete all html tags (including their attributes such as class, src, id, etc.) on a string but I would like to retain <ul>,<li> and <br /> tags. How will I accomplish this?

Any suggestion would be greatly appreciated.

Thanks!

4 Answers 4

6

Use strip_tags() and pass in allowable tags. No need to re-invent the wheel. There are some security concerns with this method which might prevent you from using it. Please consider @Denis' answer if this is the case.

Sign up to request clarification or add additional context in comments.

3 Comments

That's not a good solution, actually: stackoverflow.com/questions/5788527/…
@Denis It may be harmful, but then again we aren't told exactly how it will be used and in which way. There are many uses of this function that would be legitimate and provide no security risks whatsoever. Unfortunately the OP didn't provide sufficient details for us to make that decision.
@acidpaul Please give special attention to Denis' comments as well. Glad to see we could help you.
3

You can use a library such as html purifier:

http://htmlpurifier.org/

Be wary of using strip_tags() with an argument:

Is strip_tags() vulnerable to scripting attacks?

http://htmlpurifier.org/comparison

1 Comment

+1 For the warning. The user may find a simple use of strip_tags() to be less appropriate.
3

http://us.php.net/manual/en/function.strip-tags.php

strip_tags($text, '<ul><li><br>');

Comments

1

There you go, bro

http://us.php.net/manual/en/function.strip-tags.php

$text = strip_tags($text, '<ul><li><br>');

Do not use preg_replace, it cost u lot memory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.