HTML转纯文本,求吐槽

首先吐槽一下iteye的问卷。
然后,闲的蛋疼做了一个HTML转纯文本的小工具类。
当然功能上只保留了一些最简单的例如<br>,<p>,<div>把它们转换成\n。



package com.yyt.core.utils.net.ubb;

import java.util.regex.Pattern;

public class HTMLUtils {

private static final Pattern HTML_CHECKER = Pattern
.compile("</[a-z][^>]*>|<[a-z][^/>]*/>");

public static String toText(String html) {

if (html == null)
return null;

boolean is = isHtml(html);

if (is) {
html = html
.replaceAll("\r?\n", "")
.replaceAll(
"<[sS][tT][yY][lL][eE] [^>]+>[\\d\\D]+?</[sS][tT][yY][lL][eE]>",
"")
.replaceAll(
"<[sS][tT][yY][lL][eE]>[\\d\\D]+?</[sS][tT][yY][lL][eE]>",
"")
.replaceAll(
"<[sS][cC][rR][iI][pP][tT] [^>]+>[\\d\\D]+?</[sS][cC][rR][iI][pP][tT]>",
"")
.replaceAll(
"<[sS][cC][rR][iI][pP][tT]>[\\d\\D]+?</[sS][cC][rR][iI][pP][tT]>",
"")
.replaceAll(
"<[tT][aA][bB][lL][eE] [^>]+>[\\d\\D]+?</[tT][aA][bB][lL][eE]>",
"")
.replaceAll(
"<[tT][aA][bB][lL][eE]>[\\d\\D]+?</[tT][aA][bB][lL][eE]>",
"")
.replaceAll("\n*(<(?!\\s)[^>\n]+>)\n*", "$1")
.replaceAll(
"(</?[bB][rR]\\s*+/?[^>]*+>|</?[pP][^>]*+>|</[dD][iI][vV][^>]*+>|</[lL][iI][^>]*+>)\\s*+",
"\n");
}

return html = html.replaceAll("</?(?!\\s)[a-zA-Z][^>]*>", "")
.replaceAll("<!--[\\d\\D]*?-->", "").replaceAll("\n{2,}", "\n");

}

public static boolean isHtml(String html) {
return HTML_CHECKER.matcher(html).find();
}
}


啊咧,有没有发错版块呢?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值