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("</[^>]*>|<[^/>]*/>");public static String toText(String html) {if (html == null)return null;boolean is = isHtml(html);if (is) {html = html.replaceAll("\r?\n", "").replaceAll("< [^>]+>[\\d\\D]+?</>","").replaceAll("<>[\\d\\D]+?</>","").replaceAll("< [^>]+>[\\d\\D]+?</>","").replaceAll("<>[\\d\\D]+?</>","").replaceAll("< [^>]+>[\\d\\D]+?</>","").replaceAll("<>[\\d\\D]+?</>","").replaceAll("\n*(<(?!\\s)[^>\n]+>)\n*", "$1").replaceAll("(</?\\s*+/?[^>]*+>|</?[^>]*+>|</[^>]*+>|</[^>]*+>)\\s*+","\n");}return html = html.replaceAll("</?(?!\\s)[^>]*>", "").replaceAll("<!--[\\d\\D]*?-->", "").replaceAll("\n{2,}", "\n");}public static boolean isHtml(String html) {return HTML_CHECKER.matcher(html).find();}}
啊咧,有没有发错版块呢?
页:
[1]