Skip to content
Advertisement

strip_tags in php doesnt help, need a better alternative

I got a form value in which I need to strip html tags and not the other things.

Example: I pass following in textarea

<h1> test </h1> Testing <5 years or >2 years.

I need to get following outcome

test Testing <5 years or >2 years.

When I try strip tags it removed 5 years also which I dont want. Many thanks for those who help on this.!

Advertisement

Answer

You need to pre-process the HTML to convert the open and closing angle brackets that do not comprise valid HTML tags to entities prior to calling strip_tags. There are a number of ways to do this, but the easiest and most reliable is probably to use HTML Purifier. HTML purifier is smart enough to entity encode the greater-than and less-than in your string, while leaving the H1 tags intact. You can then strip tags, and if you need to have the angle brackets restored, use html_entity_decode.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement