How to remove or remove html tags in Android

PHP has a strip_tags function that removes HTML and PHP tags from a string.

Does Android have a way to avoid html?

+62
android strip-tags
Jun 28 2018-11-11T00:
source share
6 answers

The solutions in the answer related to @sparkymat usually require either a regular expression that is prone to approach errors or the installation of a third-party library such as jsoup or jericho . The best solution for Android devices is to use 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); } else { return Html.fromHtml(html); } } 

This uses the Android built-in Html parser to create a Spanned view of the input html without any html tags. The Span markup is then removed by converting the output back to a string.

As discussed here , the behavior of Html.fromHtml has changed from Android N. See the documentation for more information.

+191
Sep 12 2018-11-12T00:
source share

Alternatively, you can use Html.escapeHtml(String) if you are targeting API 16 or higher.

For targeting below API 16, you can instead use the following class by calling HtmlUtils.escapeHtml(String) , which I just pulled from the source Html.escapeHtml(String) .

 public class HtmlUtils { public static String escapeHtml(CharSequence text) { StringBuilder out = new StringBuilder(); withinStyle(out, text, 0, text.length()); return out.toString(); } private static void withinStyle(StringBuilder out, CharSequence text, int start, int end) { for (int i = start; i < end; i++) { char c = text.charAt(i); if (c == '<') { out.append("&lt;"); } else if (c == '>') { out.append("&gt;"); } else if (c == '&') { out.append("&amp;"); } else if (c >= 0xD800 && c <= 0xDFFF) { if (c < 0xDC00 && i + 1 < end) { char d = text.charAt(i + 1); if (d >= 0xDC00 && d <= 0xDFFF) { i++; int codepoint = 0x010000 | (int) c - 0xD800 << 10 | (int) d - 0xDC00; out.append("&#").append(codepoint).append(";"); } } } else if (c > 0x7E || c < ' ') { out.append("&#").append((int) c).append(";"); } else if (c == ' ') { while (i + 1 < end && text.charAt(i + 1) == ' ') { out.append("&nbsp;"); i++; } out.append(' '); } else { out.append(c); } } } } 

I am using this class that works great.

+8
Sep 17 '15 at 0:33
source share

Sorry for posting late, but I think this may help others,

To just remove the html bands

 Html.fromHtml(htmltext).toString() 

Thus, the html tag will be replaced with a string, but the string will not be formatted properly. Therefore i did

 Html.fromHtml(htmltext).toString().replaceAll("\n", "").trim() 

Thus, I first replace the next line with a space and remove the empty space. Similarly, you can remove others.

+7
Jul 07 '16 at 9:10
source share

This is for a new method alternative (API 16 +):

 android.text.Html.escapeHtml(your_html).toString(); 
+4
Jan 26 '17 at 6:30
source share
  Spanned spanned; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { spanned = Html.fromHtml(textToShare, Html.FROM_HTML_MODE_LEGACY); } else { spanned = Html.fromHtml(textToShare); } tv.setText(spanned.toString()); 
+1
Nov 15 '16 at 7:18
source share

It's dead just with jsoup

 public static String html2text(String html) { return Jsoup.parse(html).text(); } 
0
Jul 10 '17 at 10:29 on
source share



All Articles