How to strip or escape html tags in Android

Viewed 62483

PHP has strip_tags function which strips HTML and PHP tags from a string.

Does Android have a way to escape html?

8 Answers

As it has not been mentioned yet, the way to do this in a backwards compatible manner would be to use the HtmlCompat utility class, and simply call (with 0 if you require no specific flags to be used)

HtmlCompat.from(inputString, 0).toString()

Under the hood it already does all the required api checks for you

if (Build.VERSION.SDK_INT >= 24) {
   return Html.fromHtml(source, flags);
}
return Html.fromHtml(source);

So for for the input

<a href="https://www.stackoverflow.com">Click me!</a>

you will receive only the string 'Click me!' as output.

Related