|
发表于 2004-11-7 22:58:30
|
显示全部楼层
这是我以前借鉴别人的代码后,写的一个,兄弟也许可以参考一下
[PHP]
package com.tony;
public class HtmlFilter{
public static final String convertUBB(String input) {
if (input == null){ // || input.length() == 0 || (input.indexOf("[", 0) < 0 && input.indexOf("]", 0) < 0)) {
input = "";
return input;
} else {
input = HtmlFilter.escapeHTMLTag(input);
input = HtmlFilter.replace(input, "", "<b>");
input = HtmlFilter.replace(input, "", "</b>");
input = HtmlFilter.replace(input, "", "<i>");
input = HtmlFilter.replace(input, "", "</i>");
input = HtmlFilter.replace(input, "", "<u>");
input = HtmlFilter.replace(input, "", "</u>");
input = HtmlFilter.replace(input, " ", " ");
input = HtmlFilter.replace(input, "\n", "<br>");
input = HtmlFilter.convertSpecialTagSingle(
input,
"", "",
"<a href=\"", "\">", "</a>",
"[/");
input = HtmlFilter.convertSpecialTagSingle(
input,
"[mail]", "[/mail]",
"<a href=\"mailto:", "\">", "</a>",
"[/");
input =
HtmlFilter.convertSpecialTagSingle(
input,
"",
"<img src=\"", "\">", "</img>",
"[/");
input =
HtmlFilter.convertSpecialTagSingle(
input,
"[music]", "[/music]",
"<embed src=\"","\" autostart=\"true\" loop=\"true\" width=\"1\" height=\"1\">","</embed>",
"[/");
input = HtmlFilter.convertSpecialTag(
input,
"[center", "",
"<div align=\"center", "\">", "</div>",
"]");
input = HtmlFilter.convertSpecialTag(
input,
"[url=", "[/url]",
"<a href=\"", "\">", "</a>",
"]");
input =
HtmlFilter.convertSpecialTag(
input,
"[font color=", "",
"<font color=\"", "\">", "</font>",
"]");
input =
HtmlFilter.convertSpecialTag(
input,
"[color=", "",
"<font color=\"", "\">", "</font>",
"]");
input =
HtmlFilter.convertSpecialTag(
input,
"[font size=","",
"<font size=\"","\">", "</font>",
"]");
input =
HtmlFilter.convertSpecialTag(
input,
"[size=","",
"<font size=\"","\">", "</font>",
"]");
input =
HtmlFilter.convertSpecialTag(
input,
"[mail=", "[/mail]",
"<a href=\"mailto:", "\">", "</a>",
"]");
input =
HtmlFilter.convertSpecialTag(
input,
"[img=", "[/img]",
"<img src=\"", "\">", "</img>",
"]");
input = HtmlFilter.convertSpecialTag(
input,
"[fly", "[/fly]",
"<marquee width=90% behavior=alternate scrollamount=3", ">", "</marquee>",
"]");
input = HtmlFilter.convertSpecialTag(
input,
"[move", "[/move]",
"<marquee width=90% behavior=alternate scrollamount=3", ">", "</marquee>",
"]");
input = HtmlFilter.convertSpecialTag(
input,
"[music=", "[/music]",
"<embed src=\"","\" autostart=\"true\" loop=\"true\" width=\"1\" height=\"1\">","</embed>",
"]");
return input;
}
}
public static final String escapeHTMLTag(String input) {
if(input == null){
input = "";
return input;
}
input = replace(input.trim(),"&","&");
input = replace(input.trim(),"<","<");
input = replace(input.trim(),">",">");
input = replace(input.trim(),"\t"," ");
input = replace(input.trim(),"\r\n","\n");
input = replace(input.trim(),"\n","<br>");
input = replace(input.trim()," "," ");
input = replace(input.trim(),"'","'");
input = replace(input.trim(),"\\","\");
return input;
}
public static final String replace(
String input,
String oldString,
String newString) {
if (input == null|| input.length() == 0|| input.indexOf(oldString, 0) < 0) {
return input;
} else {
int i = 0;
int j = 0;
int oldLength = oldString.length();
StringBuffer buffer = new StringBuffer();
while ((i = input.indexOf(oldString, i)) >= 0) {
buffer.append(input.substring(j, i)).append(newString);
j = (i += oldLength);
}
input = buffer.append(input.substring(j)).toString();
buffer = null;
return input;
}
}
private static final String convertSpecialTag(
String input,
String ubbTagPre,
String ubbTagEnd,
String htmlTagPrefix,
String htmlTagSuffix,
String htmlTagEnd,
String interval) {
if (input == null || input.length() == 0)
return input;
int i = 0;
int j = 0;
StringBuffer htmlBuffer = null;
StringBuffer ubbBuffer = null;
while ((i = input.indexOf(ubbTagPre, 0)) >= 0
&& (j = input.indexOf(interval, i)) >= 0) {
htmlBuffer = new StringBuffer(htmlTagPrefix);
ubbBuffer = new StringBuffer(ubbTagPre);
input =
replace(
replace(
input,
ubbBuffer
.append(input.substring(i + ubbTagPre.length(), j))
.append(interval)
.toString(),
htmlBuffer
.append(
input.substring(
input.indexOf(ubbTagPre, 0)
+ ubbTagPre.length(),
input.indexOf(interval, i)))
.append(htmlTagSuffix)
.toString()),
ubbTagEnd,
htmlTagEnd);
ubbBuffer = null;
htmlBuffer = null;
}
return input;
}
private static final String convertSpecialTagSingle(
String input,
String ubbTagPre,
String ubbTagEnd,
String htmlTagPrefix,
String htmlTagSuffix,
String htmlTagEnd,
String interval) {
if (input == null || input.length() == 0)
return input;
int i = 0;
int j = 0;
StringBuffer htmlBuffer = null;
StringBuffer ubbBuffer = null;
while ((i = input.indexOf(ubbTagPre, 0)) >= 0
&& (j = input.indexOf(interval, i)) >= 0) {
htmlBuffer = new StringBuffer(htmlTagPrefix);
ubbBuffer = new StringBuffer(ubbTagPre);
input =
replace(
replace(
input,
ubbBuffer
.toString(),
htmlBuffer
.append(
input.substring(
input.indexOf(ubbTagPre, 0)
+ ubbTagPre.length(),
input.indexOf(interval, i)))
.append(htmlTagSuffix)
.toString()),
ubbTagEnd,
htmlTagEnd);
ubbBuffer = null;
htmlBuffer = null;
}
/*
if (ubbTagPre == "[img]"){
StringBuffer input2 = new StringBuffer(" ");
input2.append(input.substring(0,input.indexOf("\">"))).append("1234")
.append(htmlTagEnd);
input = input2.toString();
}
*/
return input;
}
}
[/PHP] |
|