如何在Android中剥离或转义html标签

PHP具有strip_tags从字符串中剥离HTML和PHP标签的功能。


Android是否有逃脱html的方法?


Cats萌萌
浏览 468回答 3
3回答

幕布斯7119047

链接到的答案中的解决方案通常需要使用正则表达式(这是一种容易出错的方法),或者安装第三方库(例如jsoup或jericho)。在Android设备上更好的解决方案是仅使用Html.fromHtml()函数: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();    }}这使用Android内置的HTML解析器来构建Spanned没有任何html标签的输入html 的表示形式。然后,通过将输出转换回字符串来剥离“ Span”标记。随着讨论这里,Html.fromHtml行为已经改变,因为Android的N.请参阅文档获取更多信息。

吃鸡游戏

不好意思,我认为这对其他人可能会有所帮助,只需删除html条Html.fromHtml(htmltext).toString()这样,html标记将被替换为字符串,但是字符串的格式将不正确。因此我做到了Html.fromHtml(htmltext).toString().replaceAll("\n", "").trim()这样,我首先用具有空格的nextline替换并删除了空格。同样,您可以删除其他人。

料青山看我应如是

Html.escapeHtml(String)如果您定位的是API 16或更高版本,则可以选择使用。对于也在API 16以下定位的对象,您可以改为调用以下类,HtmlUtils.escapeHtml(String)而我只是从源代码中拉出该类Html.escapeHtml(String)。public class HtmlUtils {&nbsp; &nbsp; public static String escapeHtml(CharSequence text) {&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder out = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; withinStyle(out, text, 0, text.length());&nbsp; &nbsp; &nbsp; &nbsp; return out.toString();&nbsp; &nbsp; }&nbsp; &nbsp; private static void withinStyle(StringBuilder out, CharSequence text,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int start, int end) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = start; i < end; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char c = text.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (c == '<') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.append("&lt;");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (c == '>') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.append("&gt;");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (c == '&') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.append("&amp;");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (c >= 0xD800 && c <= 0xDFFF) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (c < 0xDC00 && i + 1 < end) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char d = text.charAt(i + 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (d >= 0xDC00 && d <= 0xDFFF) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int codepoint = 0x010000 | (int) c - 0xD800 << 10 | (int) d - 0xDC00;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.append("&#").append(codepoint).append(";");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (c > 0x7E || c < ' ') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.append("&#").append((int) c).append(";");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (c == ' ') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (i + 1 < end && text.charAt(i + 1) == ' ') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.append("&nbsp;");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.append(' ');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.append(c);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}我正在使用这个效果很好的课程。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android