当前位置:  编程技术>其它

<font color=green>UBB 转换函数演示 (经典论坛)

    来源: 互联网  发布时间:2014-10-12

    本文导语:  UBB代码对于经常上论坛的朋友来说是最熟悉不过了。这里我不多说了,关于怎么实现UBB转换,不了解的朋友请看这里正则表达式在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,"点击开新窗口欣赏该FLASH动画! [全屏欣赏]

$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,"

$3
")
//循环转换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,"点击开新窗口欣赏该FLASH动画! [全屏欣赏]

    $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,"

    $3
    ")
    '循环转换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



        
     
     
     
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • java命名空间java.awt类font的类成员方法: font定义及介绍
  • 正则表达式:过滤<font>和</font>
  • java命名空间java.awt类font的类成员方法: truetype_font定义及介绍
  • resin 与tomcat的区别错误?out.print("<font color=green>可用</font>");
  • java命名空间java.awt类font的类成员方法: type1_font定义及介绍
  • <font color=red>苦思不得其解的问题</font>
  • java命名空间java.awt.font类textattribute的类成员方法: font定义及介绍
  • 留言板问题,如何过滤掉html标签 防止别人引用html的标签呢??如<font color=red>测试</font>
  • java命名空间java.awt类font的类成员方法: derivefont定义及介绍
  • i have a string="<html><body><font color='red'>i want display</font></body><html>", can i display it in java?
  • java命名空间java.awt类fontmetrics的类成员方法: font定义及介绍
  • JavaScript字体工具包 Font.js
  • java命名空间javax.accessibility类accessiblerole的类成员方法: font_chooser定义及介绍
  • Eclipse 字体大小控制插件 eclipse-fonts
  • java命名空间java.awt类font的类成员方法: getnumglyphs定义及介绍
  • jquery获取font-size属性值的代码
  • java命名空间java.awt类font的类成员方法: getitalicangle定义及介绍
  • Web服务器/前端 iis7站长之家
  • java命名空间javax.swing.text.html类html.tag的类成员方法: font定义及介绍
  • 请教一个font的问题
  • java命名空间javax.swing.text.html类css.attribute的类成员方法: font定义及介绍
  • css font缩写总结附实例


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3