Java ubb转html工具类

交流 Java  收藏
0 / 76

Java ubb转html工具类,这里先收藏备份一下,以后可能会用到。

package cn.mucang.question.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UbbUtils {
    private static final String URL = "<a href='$2' target=_blank>$2</a>";
    private static final String IMG = "<img src='$2'><br>$2</img>";
    private static final String IMG1 = "<img src='$4' width=\"$2\" hight=\"$3\"><br>$4</img>";
    private static final String I = "<i>$2</i>";
    private static final String LI = "<li>$2</li>";
    private static final String B = "<b>$2</b>";
    private static final String U = "<u>$2</u>";
    private static final String H1 = "<h1>$2</h1>";
    private static final String H2 = "<h2>$2</h2>";
    private static final String H3 = "<h3>$2</h3>";
    private static final String H4 = "<h4>$2</h4>";
    private static final String H5 = "<h5>$2</h5>";
    private static final String H6 = "<h6>$2</h6>";

    public static String clearHtmlTag(String s, int much) {
        try {
            Matcher m = Pattern.compile("<([^>]*)>", Pattern.DOTALL).matcher(s);
            while (m.find()) {
                for (int i = 1; i <= m.groupCount(); i++) {
                    System.out.println("找到 = " + m.group());
                    s = s.replaceAll(m.group(), "");
                }
            }
            if (much != 0 && s.length() > much) {
                s = s.substring(0, much);
            }
            System.out.println("结果 = " + s);
        } catch (Exception ignored) {
        }
        return s;
    }

    public static String delHtmlTag(String str, int length) {
        int index1 = -1;
        int index2 = -1;
        while (true) {
            index1 = str.indexOf("<");
            index2 = str.indexOf(">", index1);
            if (index1 == -1) {
                break;
            }
            if (index2 == -1) {
                index2 = str.length();
            } else {
                index2 = index2 + 1;
            }
            str = str.substring(0, index1)
                    + str.substring(index2, str.length());

        }
        if (length != 0) {
            if (str.length() > length) {
                str = str.substring(0, length);
            }
        }
        str = str.replaceAll("\r", "<br/>");
        str = str.replaceAll("\t", "    ");
        return str;
    }


    /**
     * 转换UBB
     *
     * @param text
     * @return
     */
    public static String ubbDecode(String text) {
        text = replace(text, "url", URL);
        text = replace(text, "img", IMG);
        text = replace(text, "img=(.+?),(.+?)", "img", IMG1);
        text = replace(text, "u", U);
        text = replace(text, "i", I);
        text = replace(text, "li", LI);
        text = replace(text, "b", B);
        text = replace(text, "h1", H1);
        text = replace(text, "h2", H2);
        text = replace(text, "h3", H3);
        text = replace(text, "h4", H4);
        text = replace(text, "h5", H5);
        text = replace(text, "h6", H6);
        return text;
    }

    /**
     * 将 从数据库 取出来的文章,ubb语法转换成正常的html
     *
     * @param text
     * @param reg
     * @param replaceStr
     * @return
     */
    public static String replace(String text, String reg, String replaceStr) {
        Matcher m = Pattern.compile(
                "(\\[" + reg + "])(.[^\\[]*)(\\[/" + reg + "])",
                Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)
                .matcher(text);
        return m.replaceAll(replaceStr);
    }

    /**
     * 将 从数据库 取出来的文章,ubb语法转换成正常的html
     *
     * @param text
     * @param reg
     * @param regEnd
     * @param replaceStr
     * @return
     */
    public static String replace(String text, String reg, String regEnd, String replaceStr) {
        Matcher m = Pattern.compile(
                "(\\[" + reg + "])(.[^\\[]*)(\\[/" + regEnd + "])",
                Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)
                .matcher(text);
        return m.replaceAll(replaceStr);
    }

    public static void main(String arg[]) {
        String f = "[img=2,200]sds[/img]";
        System.out.print(UbbUtils.ubbDecode(f));
    }

}