<font color=green>UBB 转换函数演示 (经典论坛)
本文导语: UBB代码对于经常上论坛的朋友来说是最熟悉不过了。这里我不多说了,关于怎么实现UBB转换,不了解的朋友请看这里正则表达式在UBB论坛中的应用。 经典论坛的UBB转换我觉得是处理的非常好的,研究了一下它的转换...
经典论坛的UBB转换我觉得是处理的非常好的,研究了一下它的转换结果[因为没有源码:)],做了下面的演示。转换结果基本上觉得象了。
主要是针对[code]{html}这两个标签的处理,其他的UBB标签还是一般的处理方法。
下面是分别用javascript和vbscript实现的演示。自己做论坛的朋友,如果想实现类似经典论坛的运行代码框可以看看这种转换。
欢迎大家输入尽量多格式的UBB进行测试,多多指出错误。
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";
}
[i][b][color=blue]blue[/color][/b][/i]
{html}[color=red]te
st[/color]{/html}
{html}[code]te
st[/code]{/html}
[code]{html}te
st{/html}[/code]
转换
strIcons = "1" //允许笑脸转换
strSmile="1" //同上
ImgName="em"
picurl="pic/"
function UBBCode(content,html){
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]([sS]+?)[/code])/ig,"[/phx_code]$1[phx_code]");
content=content.replace(/[phx_code]([sS]*?)[/phx_code]/ig,function($1,$2){if(html=="html"){return(HTMLCode($2));}else{return(trans($2));}});
content=content.replace(/[code](rn)?([sS]+?)[/code]/ig,function($1,$2,$3){return("
"+DvbbsHtmlAn($3)+"")});
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);
}
function HTMLCode(content){
content="[phx_html]"+content+"[/phx_html]";
content=content.replace(/([html]([sS]+?)[/html])/ig,"[/phx_html]$1[phx_html]");
content=content.replace(/[phx_html]([sS]*?)[/phx_html]/ig,function($1,$2){return(trans($2))});
content=content.replace(/[html]([sS]+?)[/html]/ig,function($1,$2){return(""+hencode($2)+"
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]")});
return(content);
}
function hencode(fString){ //简单模拟server.htmlencode ASP中不用这个
fString = fString.replace(/&/ig,"&")
fString = fString.replace(/>/ig,">") //>
fString = fString.replace(/screen.width-333)this.width=screen.width-333" galleryImg=no>");
re=/[DIR=*([0-9]*),*([0-9]*)](.*?)[/DIR]/ig
strContent=strContent.replace(re,"")
re=/[QT=*([0-9]*),*([0-9]*)](.*?)[/QT]/ig
strContent=strContent.replace(re,"")
re=/[MP=*([0-9]*),*([0-9]*)](.*?)[/MP]/ig
strContent=strContent.replace(re,"")
re=/[RM=*([0-9]*),*([0-9]*)](.*?)[/RM]/ig
strContent=strContent.replace(re,"
")
re=/([FLASH])(.*?)([/FLASH])/ig
strContent= strContent.replace(re,"$2")
re=/([FLASH=*([0-9]*),*([0-9]*)])(.*?)([/FLASH])/ig
strContent= strContent.replace(re," [全屏欣赏]
$4")
re=/([UPLOAD=gif])(.*?)([/UPLOAD])/ig
strContent= strContent.replace(re,"
此主题相关图片如下:
")
re=/([UPLOAD=jpg])(.*?)([/UPLOAD])/ig
strContent= strContent.replace(re,"
此主题相关图片如下:
")
re=/([UPLOAD=bmp])(.*?)([/UPLOAD])/ig
strContent= strContent.replace(re,"
此主题相关图片如下:
")
re=/([UPLOAD=(.[^[]*)])(.*?)([/UPLOAD])/ig
strContent= strContent.replace(re,"
点击浏览该文件")
re=/([URL])(.*?)([/URL])/ig
strContent= strContent.replace(re,"$2")
re=/([URL=(.[^[]*)])(.*?)([/URL])/ig
strContent= strContent.replace(re,"$3")
re=/([EMAIL])(.*?)([/EMAIL])/ig
strContent= strContent.replace(re,"$2")
re=/([EMAIL=(.[^[]*)])(.*?)([/EMAIL])/ig
strContent= strContent.replace(re,"$3")
re =/^(http://[A-Za-z0-9./=?%-&_~`@':+!]+)/ig
strContent = strContent.replace(re,"$1")
re =/(http://[A-Za-z0-9./=?%-&_~`@':+!]+)$/ig
strContent = strContent.replace(re,"$1")
re =/[^>=""](http://[A-Za-z0-9./=?%-&_~`@':+!]+)/ig
strContent = strContent.replace(re,"$1")
re =/^(ftp://[A-Za-z0-9./=?%-&_~`@':+!]+)/ig
strContent = strContent.replace(re,"$1")
re =/(ftp://[A-Za-z0-9./=?%-&_~`@':+!]+)$/ig
strContent = strContent.replace(re,"$1")
re =/[^>=""](ftp://[A-Za-z0-9./=?%-&_~`@':+!]+)/ig
strContent = strContent.replace(re,"$1")
re =/^(rtsp://[A-Za-z0-9./=?%-&_~`@':+!]+)/ig
strContent = strContent.replace(re,"$1")
re =/(rtsp://[A-Za-z0-9./=?%-&_~`@':+!]+)$/ig
strContent = strContent.replace(re,"$1")
re =/[^>=""](rtsp://[A-Za-z0-9./=?%-&_~`@':+!]+)/ig
strContent = strContent.replace(re,"$1")
re =/^(mms://[A-Za-z0-9./=?%-&_~`@':+!]+)/ig
strContent = strContent.replace(re,"$1")
re =/(mms://[A-Za-z0-9./=?%-&_~`@':+!]+)$/ig
strContent = strContent.replace(re,"$1")
re =/[^>=""](mms://[A-Za-z0-9./=?%-&_~`@':+!]+)/ig
strContent = strContent.replace(re,"$1")
if(strIcons == "1"){ //笑脸转换
re=/([em(.*?)])/ig
strContent=strContent.replace(re,"")
}
if(strSmile == "1"){ //笑脸转换
re=/([s(d{1,2})])/ig
strContent=strContent.replace(re,"")
}
re=/([color=(.[^[]*)])(.*?)([/color])/ig
strContent=strContent.replace(re,"$3")
re=/([face=(.[^[]*)])(.*?)([/face])/ig
strContent=strContent.replace(re,"$3")
re=/([align=(.[^[]*)])(.*?)([/align])/ig
strContent=strContent.replace(re,"
//循环转换quote
re=/([QUOTE])(.*?)([/QUOTE])/ig
while(strContent.indexOf("[quote]")>0 && strContent.indexOf("[/quote] ")>0){
strContent=strContent.replace(re,"引用:$2")
}
re=/([fly])(.*?)([/fly])/ig
strContent=strContent.replace(re,"$2")
re=/([move])(.*?)([/move])/ig
strContent=strContent.replace(re,"$2")
re=/[GLOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)](.*?)[/GLOW]/ig
strContent=strContent.replace(re,"$4")
re=/[SHADOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)](.*?)[/SHADOW]/ig
strContent=strContent.replace(re,"$4")
re=/([i])(.*?)([/i])/ig
strContent=strContent.replace(re,"$2")
re=/([u])(.*?)([/u])/ig
strContent=strContent.replace(re,"$2")
re=/([b])(.*?)([/b])/ig
strContent=strContent.replace(re,"$2")
re=/([size=1])(.*?)([/size])/ig
strContent=strContent.replace(re,"$2")
re=/([size=2])(.*?)([/size])/ig
strContent=strContent.replace(re,"$2")
re=/([size=3])(.*?)([/size])/ig
strContent=strContent.replace(re,"$2")
re=/([size=4])(.*?)([/size])/ig
strContent=strContent.replace(re,"$2")
re=/[size=([+|-]?[0-7])](.*?)([/size])/ig
strContent=strContent.replace(re,"$2")
re=/([list])(.+?)([/list])/ig;
strContent=strContent.replace(re,"
- $2
re=/([list=)(A|1)(])(.+?)([/list])/ig;
strContent=strContent.replace(re,"$4");
re=/([*])/ig;
strContent=strContent.replace(re,"
re=/([center])(.*?)([/center])/ig
strContent=strContent.replace(re,"$2")
re=/([table=(#*[a-z0-9]*),(#*[a-z0-9]*),(#*[a-z0-9]*)])(.*?)([/table])/ig
strContent=strContent.replace(re,"$5")
return(strContent);
}
vbscript:
.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";
}
[i][b][color=blue]blue[/color][/b][/i]
{html}[color=red]te
st[/color]{/html}
{html}[code]te
st[/code]{/html}
[code]{html}te
st{/html}[/code]
转换
strIcons = "1" '允许笑脸转换
strSmile="1" '同上
ImgName="em"
picurl="pic/"
function UBBCode(content)
'on error resume next
st=timer
set re=new regexp
re.global=true
re.pattern="[code](rn)?([sS]+?)[/code]"
Set Matches = re.Execute(content) ' Execute search.
dim code()
n=0
if re.test(content) then codeb=true
if codeb then
For Each Match in Matches ' Iterate Matches collection.
redim Preserve code(n)
code(n) =Match.Value
n=n+1
Next
for i=0 to ubound(code)
if code(i)="" then exit for
content=replace(content,code(i),"{code"&i&"}")
next
end if
re.pattern="[html](rn)?([sS]+?)[/html]"
Set Matches = re.Execute(content) ' Execute search.
dim html()
n=0
if re.test(content) then htmlb=true
if htmlb then
For Each Match in Matches ' Iterate Matches collection.
redim Preserve html(n)
html(n) =Match.Value
n=n+1
Next
for i=0 to ubound(html)
if html(i)="" then exit for
content=replace(content,html(i),"{html"&i&"}")
next
end if
content=gmt(content) 'ubb
if codeb then
for i=0 to ubound(code)
if code(i)="" then exit for
tcode=hencode(code(i))
content=replace(content,"{code"&i&"}",tcode)
next
end if
'-----
re.pattern="[html](rn)?([sS]+?)[/html]"
Set Matches = re.Execute(content) ' Execute search.
dim html2()
n=0
if re.test(content) then html2b=true
if html2b then
For Each Match in Matches ' Iterate Matches collection.
redim Preserve html2(n)
html2(n) =Match.Value
n=n+1
Next
for i=0 to ubound(html2)
if html2(i)="" then exit for
content=replace(content,html2(i),"{html2"&i&"}")
next
end if
if htmlb then
for i=0 to ubound(html)
if html(i)="" then exit for
thtml=hencode(html(i))
content=replace(content,"{html"&i&"}",thtml)
next
end if
re.Pattern="([code])(rn)?([sS]+?)([/code])"
content=re.Replace(content,"
$3")
re.Pattern="([html])(rn)?([sS]+?)([/html])"
content=re.Replace(content,"$3
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]")
if html2b then
for i=0 to ubound(html2)
if html2(i)="" then exit for
thtml2=html2(i)
content=replace(content,"{html2"&i&"}",thtml2)
next
end if
UBBCode=content
msgbox "转换共耗时 "&formatnumber((timer-st)*1000,2)&" ms"
end function
function hencode(fString) '简单模拟server.htmlencode ASP中不用这个
fString = replace(fString,"&","&")
fString = replace(fString,">",">")
fString = replace(fString,"")
if Not UserSign or (UserSign and UserImgCode=1) then
re.Pattern="[DIR=*([0-9]*),*([0-9]*)](.*?)[/DIR]"
strContent=re.Replace(strContent,"")
re.Pattern="[QT=*([0-9]*),*([0-9]*)](.*?)[/QT]"
strContent=re.Replace(strContent,"")
re.Pattern="[MP=*([0-9]*),*([0-9]*)](.*?)[/MP]"
strContent=re.Replace(strContent,"")
re.Pattern="[RM=*([0-9]*),*([0-9]*)](.*?)[/RM]"
strContent=re.Replace(strContent,"
")
end if
re.Pattern="([FLASH])(.*?)([/FLASH])"
strContent= re.Replace(strContent,"$2")
re.Pattern="([FLASH=*([0-9]*),*([0-9]*)])(.*?)([/FLASH])"
strContent= re.Replace(strContent," [全屏欣赏]
$4")
re.Pattern="([UPLOAD=gif])(.*?)([/UPLOAD])"
strContent= re.Replace(strContent,"
此主题相关图片如下:
screen.width-333)this.width=screen.width-333"">")
re.Pattern="([UPLOAD=jpg])(.*?)([/UPLOAD])"
strContent= re.Replace(strContent,"
此主题相关图片如下:
screen.width-333)this.width=screen.width-333"">")
re.Pattern="([UPLOAD=bmp])(.*?)([/UPLOAD])"
strContent= re.Replace(strContent,"
此主题相关图片如下:
screen.width-333)this.width=screen.width-333"">")
re.Pattern="([UPLOAD=(.[^[]*)])(.*?)([/UPLOAD])"
strContent= re.Replace(strContent,"
点击浏览该文件")
re.Pattern="([URL])(.*?)([/URL])"
strContent= re.Replace(strContent,"$2")
re.Pattern="([URL=(.[^[]*)])(.*?)([/URL])"
strContent= re.Replace(strContent,"$3")
re.Pattern="([EMAIL])(.*?)([/EMAIL])"
strContent= re.Replace(strContent,"$2")
re.Pattern="([EMAIL=(.[^[]*)])(.*?)([/EMAIL])"
strContent= re.Replace(strContent,"$3")
re.Pattern = "^(http://[A-Za-z0-9./=?%-&_~`@':+!]+)"
strContent = re.Replace(strContent,"$1")
re.Pattern = "(http://[A-Za-z0-9./=?%-&_~`@':+!]+)$"
strContent = re.Replace(strContent,"$1")
re.Pattern = "(
|
)(http://[A-Za-z0-9./=?%-&_~`@':+!]+)(
|
)"
strContent = re.Replace(strContent,"$1$2$3")
re.Pattern = "^(ftp://[A-Za-z0-9./=?%-&_~`@':+!]+)"
strContent = re.Replace(strContent,"$1")
re.Pattern = "(ftp://[A-Za-z0-9./=?%-&_~`@':+!]+)$"
strContent = re.Replace(strContent,"$1")
re.Pattern = "[^>=""](ftp://[A-Za-z0-9./=?%-&_~`@':+!]+)"
strContent = re.Replace(strContent,"$1")
re.Pattern = "^(rtsp://[A-Za-z0-9./=?%-&_~`@':+!]+)"
strContent = re.Replace(strContent,"$1")
re.Pattern = "(rtsp://[A-Za-z0-9./=?%-&_~`@':+!]+)$"
strContent = re.Replace(strContent,"$1")
re.Pattern = "[^>=""](rtsp://[A-Za-z0-9./=?%-&_~`@':+!]+)"
strContent = re.Replace(strContent,"$1")
re.Pattern = "^(mms://[A-Za-z0-9./=?%-&_~`@':+!]+)"
strContent = re.Replace(strContent,"$1")
re.Pattern = "(mms://[A-Za-z0-9./=?%-&_~`@':+!]+)$"
strContent = re.Replace(strContent,"$1")
re.Pattern = "[^>=""](mms://[A-Za-z0-9./=?%-&_~`@':+!]+)"
strContent = re.Replace(strContent,"$1")
if strIcons = "1" then '笑脸转换
re.Pattern="(["&ImgName&"(.*?)])"
strContent=re.Replace(strContent,"")
end if
if strSmile = "1" then '笑脸转换
re.Pattern="([s(d{1,2})])"
strContent=re.Replace(strContent,"")
end if
re.Pattern="([color=(.[^[]*)])(.*?)([/color])"
strContent=re.Replace(strContent,"$3")
re.Pattern="([face=(.[^[]*)])(.*?)([/face])"
strContent=re.Replace(strContent,"$3")
re.Pattern="([align=(.[^[]*)])(.*?)([/align])"
strContent=re.Replace(strContent,"
'循环转换quote
re.Pattern="([QUOTE])(.*?)([/QUOTE])"
while (instr(strContent,"[quote]")>0 and instr(strContent,"[/quote]")>0)
strContent=re.Replace(strContent,"引用:$2")
wend
re.Pattern="([fly])(.*?)([/fly])"
strContent=re.Replace(strContent,"$2")
re.Pattern="([move])(.*?)([/move])"
strContent=re.Replace(strContent,"$2")
re.Pattern="[GLOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)](.*?)[/GLOW]"
strContent=re.Replace(strContent,"$4")
re.Pattern="[SHADOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)](.*?)[/SHADOW]"
strContent=re.Replace(strContent,"$4")
re.Pattern="([i])(.*?)([/i])"
strContent=re.Replace(strContent,"$2")
re.Pattern="([u])(.*?)([/u])"
strContent=re.Replace(strContent,"$2")
re.Pattern="([b])(.*?)([/b])"
strContent=re.Replace(strContent,"$2")
re.Pattern="([size=1])(.*?)([/size])"
strContent=re.Replace(strContent,"$2")
re.Pattern="([size=2])(.*?)([/size])"
strContent=re.Replace(strContent,"$2")
re.Pattern="([size=3])(.*?)([/size])"
strContent=re.Replace(strContent,"$2")
re.Pattern="([size=4])(.*?)([/size])"
strContent=re.Replace(strContent,"$2")
re.Pattern="[size=([+|-]?[0-7])](.*?)([/size])"
strContent=re.Replace(strContent,"$2")
re.Pattern="([center])(.*?)([/center])"
strContent=re.Replace(strContent,"$2")
re.Pattern="([table=(#*[a-z0-9]*),(#*[a-z0-9]*),(#*[a-z0-9]*)])(.*?)([/table])"
strContent=re.Replace(strContent,"$5")
'{HTML} start
're.Pattern="([html])(.*?)([/html])"
'strContent=re.Replace(strContent,"$2
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]")
'{/HTML}end
set re=Nothing
gmt=strContent
end function