Kris Kris - 1 year ago 131
Android Question

How to strip or escape html tags in Android

PHP has

function which strips HTML and PHP tags from a string.

Does Android have a way to escape html?

Answer Source

The solutions in the answer linked to by @sparkymat generally require either regex - which is an error-prone approach - or installing a third-party library such as jsoup or jericho. A better solution on Android devices is just to make use of the Html.fromHtml() function:

public String stripHtml(String html) {
    return Html.fromHtml(html).toString();

This uses Android's built in Html parser to build a Spanned representation of the input html without any html tags. The "Span" markup is then stripped by converting the output back into a string.