📄 ubb.java
字号:
package tarena.global;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* UBB代码转换类,负责HTML代码与UBB代码之间的转换。
* 在本项目中,HTML与UBB的转换是使用的javascript脚本完成,没有使用到此类。
* 本类只是提供用java实现UBB到HTML转换的一个参考。
*/
public class UBB {
//超链接转义
private static String URL = "<a href='$2' target=_blank>$3</a>";
//无参数图片转义
private static String IMG = "<img src='$2'></img>";
//参数图片转义
private static String IMG1 ="<img src='$4' width=\"$2\" hight=\"$3\"></img>";
private static String ALIGN = "<p align=\"$2\">$3</a>";
private static String I = "<i>$2</i>";
private static String LI = "<li>$2</li>";
private static String B = "<b>$2</b>";
private static String U = "<u>$2</u>";
private static String H1 = "<h1>$2</h1>";
private static String H2 = "<h2>$2</h2>";
private static String H3 = "<h3>$2</h3>";
private static String H4 = "<h4>$2</h4>";
private static String H5 = "<h5>$2</h5>";
private static String H6 = "<h6>$2</h6>";
/**
* 转换UBB为HTML代码
*
* @param text
* @return
*/
public static String decode(String text) {
text = replace(text, "align=(.+?)","align",ALIGN);
text = replace(text, "url=(.+?)","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 length
* @return
*/
public static String replace(String text, String reg, String replaceStr) {
Matcher m = null;
m = Pattern.compile(
"(\\[" + reg + "\\])(.[^\\[]*)(\\[/" + reg + "\\])",
Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)//使用的模式:DOTALL、忽略大小写、多行
.matcher(text);
text = m.replaceAll(replaceStr);
return text;
}
/**
* 将 从数据库 取出来的文章,ubb语法转换成正常的html
*
* @param text
* @param length
* @return
*/
public static String replace(String text, String reg, String regEnd, String replaceStr) {
Matcher m = null;
m = Pattern.compile(
"(\\[" + reg + "\\])(.[^\\[]*)(\\[/" + regEnd + "\\])",
Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)//使用的模式:DOTALL、忽略大小写、多行
.matcher(text);
text = m.replaceAll(replaceStr);
return text;
}
public static void main(String arg[]) {
// String f = "[img=2,200]sds[/img]";
String f="[align=center][img]images/pic.gif[/img]666[/align]";
System.out.print(UBB.decode(f));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -