androidstrip-tags

How to strip or escape html tags in Android


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

Does Android have a way to escape html?


Solution

  • 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) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
           return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY).toString();
        } else {
           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.

    As discussed here, Html.fromHtml behaviour has changed since Android N. See the documentation for more info.