UBB代码对于经常上论坛的朋友来说是最熟悉不过了。这里我不多说了,关于怎么实现UBB转换,不了解的朋友请看这里正则表达式在UBB论坛中的应用。
经典论坛的UBB转换我觉得是处理的非常好的,研究了一下它的转换结果[因为没有源码:)],做了下面的演示。转换结果基本上觉得象了。
主要是针对[code]{html}这两个标签的处理,其他的UBB标签还是一般的处理方法。
下面是分别用javascript和vbscript实现的演示。自己做论坛的朋友,如果想实现类似经典论坛的运行代码框可以看看这种转换。
欢迎大家输入尽量多格式的UBB进行测试,多多指出错误。 复制代码 代码如下: <style> <TEXTAREA name=phx cols=65 rows=12> {html}[color=red]te {html}[code]te [code]{html}te function UBBCode(content,html){ function HTMLCode(content){ function hencode(fString){ //简单模拟server.htmlencode ASP中不用这个 function DvbbsHtmlAn(fString){ if(fString=="")return(""); fString=hencode(fString) fString = fString.replace(/\t/ig," ") //Tab function trans(strContent){
javascirpt:
.CodeSamp {
PADDING-RIGHT: 0.5em; MARGIN-TOP: 1em; PADDING-LEFT: 0.5em; FONT-SIZE: 8p; MARGIN-BOTTOM: 1em; PADDING-BOTTOM: 0.5em; MARGIN-LEFT: 0pt; PADDING-TOP: 0.5em; FONT-FAMILY: "Courier New"; BACKGROUND-COLOR: #eeeeee;width: 480px;overflow:hidden;border: 1px solid #888888;
}
textarea {
border-width: 1; border-color: #000000;font-size: 12px;FONT-FAMILY: "Tahoma", "MS Shell Dlg";
}
</style>
[i][b][color=blue]blue[/color][/b][/i]
st[/color]{/html}
st[/code]{/html}
st{/html}[/code]</textarea>
<br><button onclick=show.innerHTML=UBBCode(phx.value,"html")>转换</button><br>
<div id=show></div>
<script>
strIcons = "1" //允许笑脸转换
strSmile="1" //同上
ImgName="em"
picurl="pic/"
st=new Date().getTime()
content=content.replace(/\[(\/)?phx_code\]/ig,"{$1phx_code}");
content=content.replace(/\[(\/)?phx_html\]/ig,"{$1phx_html}");
content="[phx_code]"+content+"[/phx_code]";
content=content.replace(/(\[code\]([\s\S]+?)\[\/code\])/ig,"[/phx_code]$1[phx_code]");
content=content.replace(/\[phx_code\]([\s\S]*?)\[\/phx_code\]/ig,function($1,$2){if(html=="html"){return(HTMLCode($2));}else{return(trans($2));}});
content=content.replace(/\[code\](\r\n)?([\s\S]+?)\[\/code\]/ig,function($1,$2,$3){return("<PRE class=CodeSamp>"+DvbbsHtmlAn($3)+"</PRE>")});
content=content.replace(/\{(\/)?phx_code\}/ig,"[$1phx_code]");
content=content.replace(/\{(\/)?phx_html\}/ig,"[$1phx_html]");
alert('共耗时 '+eval(new Date().getTime()-st)+' ms');
return(content);
}
content="[phx_html]"+content+"[/phx_html]";
content=content.replace(/(\[html\]([\s\S]+?)\[\/html\])/ig,"[/phx_html]$1[phx_html]");
content=content.replace(/\[phx_html\]([\s\S]*?)\[\/phx_html\]/ig,function($1,$2){return(trans($2))});
content=content.replace(/\[html\]([\s\S]+?)\[\/html\]/ig,function($1,$2){return("<span><TEXTAREA cols=95 rows=12>"+hencode($2)+"</TEXTAREA><br><INPUT onclick=runCode() type=button value=运行代码>[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]</span>")});
return(content);
}
fString = fString.replace(/&/ig,"&")
fString = fString.replace(/>/ig,">") //>
fString = fString.replace(/</ig,"<") //<
return fString;
}
fString = fString.replace(/"/ig,""") //"
fString = fString.replace(/'/ig,"'") //'
fString = fString.replace(/\r/ig,"") //回车是一个13+10
fString = fString.replace(/\n\n/ig,"<P>") //
fString = fString.replace(/\n/ig,"<BR>") //换行
fString = fString.replace(/\x20/ig," ") //空格
return(fString);
}
var re;
strContent=DvbbsHtmlAn(strContent);
re=https://www.jb51.net/\[IMG\](.+?)\[\/IMG\]/ig;
strContent=strContent.replace(re,"<a href=https://www.jb51.net/article/$1 target=_blank><IMG SRC=https://www.jb51.net/article/$1 border=0 alt=按此在新窗口浏览图片 onload=\"javascript:if(this.width>screen.width-333)this.width=screen.width-333\" galleryImg=no></a>");
re=https://www.jb51.net/\[DIR=*([0-9]*),*([0-9]*)\](.*?)\[\/DIR]/ig
strContent=strContent.replace(re,"<object classid=clsid:166B1BCA-3F9C-11CF-8075-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,2,0 width=https://www.jb51.net/article/$1 height=https://www.jb51.net/article/$2><param name=src value=https://www.jb51.net/article/$3><embed src=https://www.jb51.net/article/$3 pluginspage=http://www.macromedia.com/shockwave/download/ width=https://www.jb51.net/article/$1 height=https://www.jb51.net/article/$2></embed></object>")
re=https://www.jb51.net/\[QT=*([0-9]*),*([0-9]*)\](.*?)\[\/QT]/ig
strContent=strContent.replace(re,"<embed src=https://www.jb51.net/article/$3 width=https://www.jb51.net/article/$1 height=https://www.jb51.net/article/$2 autoplay=true loop=false controller=true playeveryframe=false cache=false scale=TOFIT bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>")
re=https://www.jb51.net/\[MP=*([0-9]*),*([0-9]*)\](.*?)\[\/MP]/ig
strContent=strContent.replace(re,"<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=https://www.jb51.net/article/$1 height=https://www.jb51.net/article/$2 ><param name=ShowStatusBar value=-1><param name=Filename value=https://www.jb51.net/article/$3><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=https://www.jb51.net/article/$3 width=https://www.jb51.net/article/$1 height=https://www.jb51.net/article/$2></embed></object>")
re=https://www.jb51.net/\[RM=*([0-9]*),*([0-9]*)\](.*?)\[\/RM]/ig
strContent=strContent.replace(re,"<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=https://www.jb51.net/article/$1 height=https://www.jb51.net/article/$2><PARAM NAME=SRC VALUE=https://www.jb51.net/article/$3><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=true></OBJECT><br><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=https://www.jb51.net/article/$1><PARAM NAME=SRC VALUE=https://www.jb51.net/article/$3><PARAM NAME=AUTOSTART VALUE=-1><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>")