分享一个非常好用的UBB转html类

分享一个非常好用的UBB转html类。discuz搭建的论坛可以使用。

测试过没有问题的。

测试结果: <img width="423" hight="335" border=0 src=http://xxxxxxx/attachment/201108/11/166161_1313042512MPmC.jpg></img>

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

public class UbbtohtmlUtil {
	static Pattern p1=Pattern.compile("<([^>]*)>", Pattern.DOTALL);

    public static String clearHtmlTag(String s)
    {
        try
        {
            Matcher m = null;
            m = p1.matcher(s);
            while (m.find())
            {
                for (int i = 1; i <= m.groupCount(); i++)
                {
                    //System.out.println("找到 = " + m.group());
                    s = s.replaceAll(m.group(), "");
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return s.trim();
    }

    public static String delHtmlTag(String str)
    {
        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());

        }
        str = str.replaceAll("\r", "<br/>");
        str = str.replaceAll("\t", "    ");
        return str;
    }

    /**
     * 转换UBB
     * @param text
     * @return
     */
    public static String UbbDecode(String text)
    {
        text = replace(text, "tr(.*?)", "tr", "<tr>", "</tr>");

        text = replace(text, "td(.*?)", "td", "<td>", "</td>");

        text = replace(text, "table(.*?)", "table", "<table>", "</table>");

        text = replace(text, "align=(.*?)", "align", "<p align=$2>", "</align>");

        text = replace(text, "url=(.*?)", "url", "<a href='$2' target=_blank>",
                       "</a>");

        text = replace(text, "img", "img", "<img border=0 src=", "></img>");

        text = replace(text, "img=(.+?),(.+?)", "img",
                       "<img width=\"$2\" hight=\"$3\" border=0 src=",
                       "></img>");

        text = replace(text, "size=(.+?)", "size", "<font size=$2>", "</font>");

        text = replace(text, "font=(.+?)", "font", "<font face=$2>", "</font>");

        text = replace(text, "color=(.+?)", "color", "<font color=$2>",
                       "</font>");

        text = replace(text, "email=(.+?)", "email", "<a href='mailto:$2'>", "</a>");
       
        text = replace(text, "u", "<u>", "</u>");
        text = replace(text, "i", "<i>", "</i>");
        text = replace(text, "li", "<li>", "</li>");
        text = replace(text, "list=(.*?)", "<ol>", "</ol>");
        text = replace(text, "list(.*?)", "<ul>", "</ul>");
        text = replace(text, "\\*", "<li>", "</li>");
        text = replace(text, "ol", "<ol>", "</ol>");
        text = replace(text, "ul", "<ul>", "</ul>");
        text = replace(text, "b", "<b>", "</b>");
        text = replace(text, "h1", "<h1>", "</h1>");
        text = replace(text, "h2", "<h2>", "</h2>");
        text = replace(text, "h3", "<h3>", "</h3>");
        text = replace(text, "h4", "<h4>", "</h4>");
        text = replace(text, "h5", "<h5>", "</h5>");
        text = replace(text, "h6", "<h6>", "</h6>");

        return text;
    }

    public static String replace(String text, String reg, String replaceStr1,
                                 String replaceStr2)
    {
        return replace(text, reg, reg, replaceStr1, replaceStr2);
    }

    public static String replace(String text, String reg, String regEnd,
                                 String replaceStr1, String replaceStr2)
    {
        Matcher m = null;

        m = Pattern.compile(
                "(\\[" + reg + "\\])",
                Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)
            .matcher(text);
        text = m.replaceAll(replaceStr1);

        m = Pattern.compile(
                "(\\[/" + regEnd + "\\])",
                Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)
            .matcher(text);
        text = m.replaceAll(replaceStr2);

        return text;
    }
    public static void main(String arg[])
    {
        System.out.println(UbbDecode(" [img=423,335]http://XXXXXX/attachment/201108/11/166161_1313042512MPmC.jpg[/img]"));
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值