System.Text.RegularExpressions
Imports System.Collections
Imports Microsoft.VisualBasic
namespace CjjerBase
Public Class bbCode
shared public Function GetBbcode(byref str as string ) as string
Dim objregex as Regex
' Dim expressdata as string ()
' Dim replacedata as string ()
'
' expressdata = new string () { "(\[b\])(.*?)(\[\/b\])" ,"(\[i\])(.*?)(\[\/i\])","(\[code\])(.*)(\[/code\])","(\[color=(.[^\[]*)\])(.[^\[]*)(\[\/color\])"}
' replacedata = new string () { "<strong>$2</strong>", "<i>$2</i>" ,"<div class=""code"">$2</div>","<font color=$2>$3</font>"}
Dim expressdata , replacedata as ArrayList
expressdata = new ArrayList(4)'这个必添,节省资源
with expressdata
.add ("(\[b\])(.*?)(\[\/b\])")'0
.add ("(\[i\])(.*?)(\[\/i\])")
.add ("(\[code\])(.*)(\[/code\])")
.add ("(\[color=(.[^\[]*)\])(.[^\[]*)(\[\/color\])")'3
End with
replacedata = new ArrayList(4)'这个必添,节省资源
with replacedata
.add ("<strong>$2</strong>")'0
.add ("<i>$2</i>")
.add ("<div class=""code"">$2</div>")
.add ("<font color=$2>$3</font>")'3
End with
Dim e_ex as IEnumerator = expressdata.GetEnumerator
Dim e_re as IEnumerator = replacedata.GetEnumerator
Dim new_str as string = GetHtmlcode( str )
do while e_ex.movenext() and e_re.movenext()
objregex = new Regex( e_ex.Current,RegexOptions.IgnoreCase )
new_str = objregex.replace(new_str,e_re.Current )
loop
return new_str
End Function
shared public Function GetHtmlcode (byref str as string) as string
Dim new_str as string = str
' new_str = Server.HtmlEncode(str)
new_str = new_str.Replace( CHR(60), "<")'左边的<
new_str = new_str.Replace(CHR(62), ">")'右边的>
new_str = new_str.Replace( CHR(10), "<br/>")
new_str = new_str.Replace(CHR(32), " ")
new_str = new_str.Replace(CHR(9), " ")
new_str = new_str.Replace(CHR(34), """)
new_str = new_str.Replace(CHR(39), "'")
return new_str
End Function
End Class
End namespace
[Ctrl+A 全部选择 然后拷贝]
其中做了一个几个最基本的转换,读者可以调用任何数据格式加载到UBB中的,其中核心的是:
Dim objregex as Regex Dim expressdata , replacedata as ArrayList '这里可以加载你的ubb转换的正则算式 Dim e_ex as IEnumerator = expressdata.GetEnumerator Dim e_re as IEnumerator = replacedata.GetEnumerator Dim new_str as string = GetHtmlcode( str ) do while e_ex.movenext() and e_re.movenext() objregex = new Regex( e_ex.Current,RegexOptions.IgnoreCase ) new_str = objregex.replace(new_str,e_re.Current ) loop
return new_str
注意,这里我一般只使用共享方法:
shared public Function GetBbcode(byref str as string ) as string
其中传入的参数是引用,这样速度更快,因为一般的说来,需要转化的字符串较长,这点还是重要的。 此新闻共有3页 上一页 1 2 3 下一页 |