请教一个关于UBB编码转换的问题
以下是我写的ubb编码转换函数,但是发现只要数据大时,打开速度特别慢,尤其是当论坛回复的数据内容特别多,同时有用UBB标签时,打开速度将更慢,请问诸位有什么好的解决方案,提升速度呢?或者帮指出效率低下的原因:论坛地址为http://www.rfidworld.com.cn/bbs/Thread-b15.htm(这里有比较多翻页数据)
ubb编码转换Begin#region ubb编码转换Begin
/**//// <summary>
/// ubb编码转换函数
/// </summary>
/// <param name="ubb">要转换的字符串</param>
/// <returns></returns>
public static string ConvertUBB(string ubb)
{
if (string.IsNullOrEmpty(ubb))
return "";
if (ubb.IndexOf("[") == -1 || ubb.IndexOf("]") == -1)
{
return DeUrlLink(ubb).Replace("\n", "<br />");
}
ubb = DeUrl(ubb);
ubb = DeUpload(ubb);
ubb = DeImg(ubb);
ubb = DeUrlLink(ubb);
ubb = DeColor(ubb);
ubb = DeFont(ubb);
ubb = DecodeBlank(ubb);
return ubb;
}
/**//// <summary>
/// 转换[URL]标签
/// </summary>
/// <param name="ubb">要转换的字符串</param>
/// <returns></returns>
private static string DeUrl(string ubb)
{
ubb = Regex.Replace(ubb, @"(\[URL\])(.[^\[]*)(\[\/URL\])"
, "<a href=\"$2\" target=\"_blank\"><u><font color=blue>$2</font></u></a>", options);
ubb = Regex.Replace(ubb, @"(\[URL=(.[^\[]*)\])(.[^\[]*)(\[\/URL\])"
, "<a href=\"$2\" target=\"_blank\"><u><font color=blue>$3</font></u></a>", options);
ubb = Regex.Replace(ubb, @"(\[URL\])(\[\/URL\])", "", options);
ubb = Regex.Replace(ubb, @"(\[URL=(.[^\[]*)\])(\[\/URL\])", "", options);
return ubb;
}
/**//// <summary>
/// 转换[UPLOAD]标签
/// </summary>
/// <param name="ubb">要转换的字符串</param>
/// <returns></returns>
private static string DeUpload(string ubb)
{
ubb = Regex.Replace(ubb, @"\[UPLOAD=(.[^\[]*)\](.[^\[]*)\[\/UPLOAD\]"
, "<br><a href=\"$1\" title=\"点击这里下载该文件\" TARGET=\"_blank\"><img src=\"Images/1.gif\" border=\"0\" /><font color=#0000CC>$2</font></a>", options);
ubb = Regex.Replace(ubb, @"\[UPLOAD=(.[^\[]*)\]\[\/UPLOAD\]"
, "<br><a href=\"$1\" title=\"点击这里下载该文件\" TARGET=\"_blank\"><img src=\"Images/1.gif\" border=\"0\" /><font color=#0000CC>下载</font></a>", options);
ubb = Regex.Replace(ubb, @"\[UPLOAD=(.[^\[]*)\]\[\/UPLOAD\]", "", options);
ubb = ubb.Replace("Download.asp?", "Download.aspx?");
return ubb;
}
/**//// <summary>
/// 转换[IMG]标签
/// </summary>
/// <param name="ubb">要转换的字符串</param>
/// <returns></returns>
private static string DeImg(string ubb)
{
ubb = Regex.Replace(ubb, @"\[IMG\](.[^\[]*)\[\/IMG\]", "<a href=\"$1\" TARGET=\"_blank\"><img src=\"$1\" border=\"0\" onload=\"javascript:if(this.width>600)this.style.width=600;\"></a>", options);
ubb = Regex.Replace(ubb, @"\[IMG\]\[\/IMG\]", "", options);
return ubb;
}
/**//// <summary>
/// 对文章中的url进行超链接
/// </summary>
/// <param name="ubb">要转换的字符串</param>
/// <returns></returns>
private static string DeUrlLink(string ubb)
{
ubb = Regex.Replace(ubb, @"^((?:http|ftp|rtsp|mms)://((?! |"|')[A-Za-z0-9\./=\?%\-&_~`@':+!])+)"
, "<a target=\"_blank\" href=\"$1\"><font color=blue><u>$1</u></font></a>", options);
ubb = Regex.Replace(ubb, @"([^>=""])((?:http|ftp|rtsp|mms)://((?! |"|')[A-Za-z0-9\./=\?%\-&_~`@':+!])+)"
, "$1<a target=\"_blank\" href=\"$2\"><font color=blue><u>$2</u></font></a>", options);
ubb = Regex.Replace(ubb, @"^(www\.((?! |"|')[A-Za-z0-9\./=\?%\-&_~`@':+!])+)"
, "<a target=\"_blank\" href=\"http://$1\">$1</a>", options);
ubb = Regex.Replace(ubb, @"([^A-Za-z>=/\.\\""])(www\.((?! |"|')[A-Za-z0-9\./=\?%\-&_~`@':+!])+)"
, "$1<a target=\"_blank\" href=\"http://$2\"><font color=blue><u>$2</u></font></a>", options);
return ubb;
}
/**//// <summary>
/// 转换[COLOR]标签
/// </summary>
/// <param name="ubb">要转换的字符串</param>
/// <returns></returns>
private static string DeColor(string ubb)
{
ubb = Regex.Replace(ubb, @"\[color=(.[^\[]*)\](.[^\[]*)\[\/color\]", "<span style=\"color:$1\">$2</span>", options);
ubb = Regex.Replace(ubb, @"\[color=(.[^\[]*)\]\[\/color\]", "", options);
return ubb;
}
/**//// <summary>
/// 转换字体标签
/// </summary>
/// <param name="ubb">要转换的字符串</param>
/// <returns></returns>
private static string DeFont(string ubb)
{
ubb = Regex.Replace(ubb, @"\[i\](.[^\[]*)\[\/i\]", "<span style=\"font-style:italic\">$1</span>", options);
ubb = Regex.Replace(ubb, @"\[i\]\[\/i\]", "", options);
ubb = Regex.Replace(ubb, @"\[u\](.[^\[]*)\[\/u\]", "<span style=\"text-decoration:underline\">$1</span>", options);
ubb = Regex.Replace(ubb, @"\[u\]\[\/u\]", "", options);
ubb = Regex.Replace(ubb, @"\[b\](.[^\[]*)\[\/b\]", "<span style=\"font-weight:bold\">$1</span>", options);
ubb = Regex.Replace(ubb, @"\[b\](\[\/b\])", "", options);
ubb = Regex.Replace(ubb, @"\[b\]\[\/b\]", "", options);
ubb = Regex.Replace(ubb, @"\[QUOTE\](.[^\[]*)\[\/QUOTE\]"
, "<table style=\"width:100%\" cellpadding=5 cellspacing=1><TR><TD style='border-right: #cccccc 1px solid; border-top:#cccccc 1px solid; border-left: #cccccc 1px solid; border-bottom: #cccccc 1px solid;background-color:#f6f6f6' width=\"100%\">$1</td></tr></table>", options);
ubb = Regex.Replace(ubb, @"\[QUOTE\]\[\/QUOTE\]", "", options);
ubb = Regex.Replace(ubb, @"\[EDIT\](.[^\[]*)\[\/EDIT\]",
"<table align=center style=\"width:100%\" cellpadding=5 cellspacing=1><TR><TD align=left style='border-right: #f6f6f6 1px solid; border-top:#f6f6f6 1px solid; border-left: #f6f6f6 1px solid; border-bottom: #f6f6f6 1px solid;background-color:#f6f6f6' width=\"100%\">$1</td></tr></table>", options);
ubb = Regex.Replace(ubb, @"\[EDIT\]\[\/EDIT\]", "", options);
ubb = Regex.Replace(ubb, @"<(.*?)(?:javascript|vbscript)\s*:(.*?)>", "<$1$2>", options);
ubb = Regex.Replace(ubb, @"(<.*?style=.*?):expression(.*?>)", "$1$2", options);
return ubb;
}
#endregion ubb编码转换End
/**//// <summary>
/// ubb编码转换函数
/// </summary>
/// <param name="ubb">要转换的字符串</param>
/// <returns></returns>
public static string ConvertUBB(string ubb)
{
if (string.IsNullOrEmpty(ubb))
return "";
if (ubb.IndexOf("[") == -1 || ubb.IndexOf("]") == -1)
{
return DeUrlLink(ubb).Replace("\n", "<br />");
}
ubb = DeUrl(ubb);
ubb = DeUpload(ubb);
ubb = DeImg(ubb);
ubb = DeUrlLink(ubb);
ubb = DeColor(ubb);
ubb = DeFont(ubb);
ubb = DecodeBlank(ubb);
return ubb;
}
/**//// <summary>
/// 转换[URL]标签
/// </summary>
/// <param name="ubb">要转换的字符串</param>
/// <returns></returns>
private static string DeUrl(string ubb)
{
ubb = Regex.Replace(ubb, @"(\[URL\])(.[^\[]*)(\[\/URL\])"
, "<a href=\"$2\" target=\"_blank\"><u><font color=blue>$2</font></u></a>", options);
ubb = Regex.Replace(ubb, @"(\[URL=(.[^\[]*)\])(.[^\[]*)(\[\/URL\])"
, "<a href=\"$2\" target=\"_blank\"><u><font color=blue>$3</font></u></a>", options);
ubb = Regex.Replace(ubb, @"(\[URL\])(\[\/URL\])", "", options);
ubb = Regex.Replace(ubb, @"(\[URL=(.[^\[]*)\])(\[\/URL\])", "", options);
return ubb;
}
/**//// <summary>
/// 转换[UPLOAD]标签
/// </summary>
/// <param name="ubb">要转换的字符串</param>
/// <returns></returns>
private static string DeUpload(string ubb)
{
ubb = Regex.Replace(ubb, @"\[UPLOAD=(.[^\[]*)\](.[^\[]*)\[\/UPLOAD\]"
, "<br><a href=\"$1\" title=\"点击这里下载该文件\" TARGET=\"_blank\"><img src=\"Images/1.gif\" border=\"0\" /><font color=#0000CC>$2</font></a>", options);
ubb = Regex.Replace(ubb, @"\[UPLOAD=(.[^\[]*)\]\[\/UPLOAD\]"
, "<br><a href=\"$1\" title=\"点击这里下载该文件\" TARGET=\"_blank\"><img src=\"Images/1.gif\" border=\"0\" /><font color=#0000CC>下载</font></a>", options);
ubb = Regex.Replace(ubb, @"\[UPLOAD=(.[^\[]*)\]\[\/UPLOAD\]", "", options);
ubb = ubb.Replace("Download.asp?", "Download.aspx?");
return ubb;
}
/**//// <summary>
/// 转换[IMG]标签
/// </summary>
/// <param name="ubb">要转换的字符串</param>
/// <returns></returns>
private static string DeImg(string ubb)
{
ubb = Regex.Replace(ubb, @"\[IMG\](.[^\[]*)\[\/IMG\]", "<a href=\"$1\" TARGET=\"_blank\"><img src=\"$1\" border=\"0\" onload=\"javascript:if(this.width>600)this.style.width=600;\"></a>", options);
ubb = Regex.Replace(ubb, @"\[IMG\]\[\/IMG\]", "", options);
return ubb;
}
/**//// <summary>
/// 对文章中的url进行超链接
/// </summary>
/// <param name="ubb">要转换的字符串</param>
/// <returns></returns>
private static string DeUrlLink(string ubb)
{
ubb = Regex.Replace(ubb, @"^((?:http|ftp|rtsp|mms)://((?! |"|')[A-Za-z0-9\./=\?%\-&_~`@':+!])+)"
, "<a target=\"_blank\" href=\"$1\"><font color=blue><u>$1</u></font></a>", options);
ubb = Regex.Replace(ubb, @"([^>=""])((?:http|ftp|rtsp|mms)://((?! |"|')[A-Za-z0-9\./=\?%\-&_~`@':+!])+)"
, "$1<a target=\"_blank\" href=\"$2\"><font color=blue><u>$2</u></font></a>", options);
ubb = Regex.Replace(ubb, @"^(www\.((?! |"|')[A-Za-z0-9\./=\?%\-&_~`@':+!])+)"
, "<a target=\"_blank\" href=\"http://$1\">$1</a>", options);
ubb = Regex.Replace(ubb, @"([^A-Za-z>=/\.\\""])(www\.((?! |"|')[A-Za-z0-9\./=\?%\-&_~`@':+!])+)"
, "$1<a target=\"_blank\" href=\"http://$2\"><font color=blue><u>$2</u></font></a>", options);
return ubb;
}
/**//// <summary>
/// 转换[COLOR]标签
/// </summary>
/// <param name="ubb">要转换的字符串</param>
/// <returns></returns>
private static string DeColor(string ubb)
{
ubb = Regex.Replace(ubb, @"\[color=(.[^\[]*)\](.[^\[]*)\[\/color\]", "<span style=\"color:$1\">$2</span>", options);
ubb = Regex.Replace(ubb, @"\[color=(.[^\[]*)\]\[\/color\]", "", options);
return ubb;
}
/**//// <summary>
/// 转换字体标签
/// </summary>
/// <param name="ubb">要转换的字符串</param>
/// <returns></returns>
private static string DeFont(string ubb)
{
ubb = Regex.Replace(ubb, @"\[i\](.[^\[]*)\[\/i\]", "<span style=\"font-style:italic\">$1</span>", options);
ubb = Regex.Replace(ubb, @"\[i\]\[\/i\]", "", options);
ubb = Regex.Replace(ubb, @"\[u\](.[^\[]*)\[\/u\]", "<span style=\"text-decoration:underline\">$1</span>", options);
ubb = Regex.Replace(ubb, @"\[u\]\[\/u\]", "", options);
ubb = Regex.Replace(ubb, @"\[b\](.[^\[]*)\[\/b\]", "<span style=\"font-weight:bold\">$1</span>", options);
ubb = Regex.Replace(ubb, @"\[b\](\[\/b\])", "", options);
ubb = Regex.Replace(ubb, @"\[b\]\[\/b\]", "", options);
ubb = Regex.Replace(ubb, @"\[QUOTE\](.[^\[]*)\[\/QUOTE\]"
, "<table style=\"width:100%\" cellpadding=5 cellspacing=1><TR><TD style='border-right: #cccccc 1px solid; border-top:#cccccc 1px solid; border-left: #cccccc 1px solid; border-bottom: #cccccc 1px solid;background-color:#f6f6f6' width=\"100%\">$1</td></tr></table>", options);
ubb = Regex.Replace(ubb, @"\[QUOTE\]\[\/QUOTE\]", "", options);
ubb = Regex.Replace(ubb, @"\[EDIT\](.[^\[]*)\[\/EDIT\]",
"<table align=center style=\"width:100%\" cellpadding=5 cellspacing=1><TR><TD align=left style='border-right: #f6f6f6 1px solid; border-top:#f6f6f6 1px solid; border-left: #f6f6f6 1px solid; border-bottom: #f6f6f6 1px solid;background-color:#f6f6f6' width=\"100%\">$1</td></tr></table>", options);
ubb = Regex.Replace(ubb, @"\[EDIT\]\[\/EDIT\]", "", options);
ubb = Regex.Replace(ubb, @"<(.*?)(?:javascript|vbscript)\s*:(.*?)>", "<$1$2>", options);
ubb = Regex.Replace(ubb, @"(<.*?style=.*?):expression(.*?>)", "$1$2", options);
return ubb;
}
#endregion ubb编码转换End