如何剥离或逃生的HTML标签的Andr​​oid标签、HTML、oid、Andr

2023-09-12 23:37:59 作者:深愛過頭

PHP有用strip_tags 函数地带HTML和字符串中的PHP代码。

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

是否必须的android逃脱HTML?

Does android have a means to escape html?

推荐答案

在答案的解决方案,通过@sparkymat链接到一般要求要么正则表达式 - 这是一个容易出错的方法 - 或安装第三方库,如< A HREF =htt​​p://jsoup.org/> jsoup 或杰里科。在Android设备上更好的解决办法就是要利用Html.fromHtml()函数的:

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();
}

本使用内置的HTML解析器来构建一个跨区重新输入HTML的presentation没有任何html标签Android的。而跨度的标记,然后通过转换输出回字符串剥离。

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.