最近我又开始用c#做论坛,在网上找了一下,虽然也有关于ubb(c#)的转换代码,可是同样都不全面,我在这里补充了一下,拿出来和大家共享。
有什么问题到我个人的论坛
www.hushiyu.com来交流,随时欢迎。
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace myluntan
{
/// <summary>
/// UBB 的摘要说明。
/// </summary>
public class UBB
{
public UBB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region 公共静态方法
/// <summary>
/// UBB代码处理函数
/// </summary>
/// <param name="sDetail">输入字符串</param>
/// <returns>输出字符串</returns>
public string UBBToHTML(string sDetail)
{
Regex r;
Match m;
#region 处理空格
sDetail = sDetail.Replace(" "," ");
#endregion
#region 处理单引号
sDetail = sDetail.Replace("'","’");
#endregion
#region 处理双引号
sDetail = sDetail.Replace("\"",""");
#endregion
#region html标记符
sDetail = sDetail.Replace("<","<");
sDetail = sDetail.Replace(">",">");
#endregion
#region 处理换行
//处理换行,在每个新行的前面添加两个全角空格
r = new Regex(@"(\r\n(( )| )+)(?<正文> +)",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"<BR> " + m.Groups["正文"].ToString());
}
//处理换行,在每个新行的前面添加两个全角空格
sDetail = sDetail.Replace("\r\n","<BR>");
#endregion
#region 处标记
r = new Regex(@"(\[b\])([ \t]*?)(\[\/b\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"<B>" + m.Groups[2].ToString() + "</B>");
}
#endregion
#region 处标记
r = new Regex(@"(\[i\])([ \t]*?)(\[\/i\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"<I>" + m.Groups[2].ToString() + "</I>");
}
#endregion
#region 处标记
r = new Regex(@"(\[U\])([ \t]*?)(\[\/U\])",RegexOptions.IgnoreCase);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"<U>" + m.Groups[2].ToString() + "</U>");
}
#endregion
#region 处[p][/p]标记
//处[p][/p]标记
r = new Regex(@"((\r\n)*\[p\])(.*?)((\r\n)*\[\/p\])",RegexOptions.IgnoreCase|RegexOptions.Singleline);
for (m = r.Match(sDetail); m.Success; m = m.NextMatch())
{
sDetail = sDetail.Replace(m.Groups[0].ToString(),"<P class=\"pstyle\">" + m.Groups[3].ToString() + "</P>");
}
#endregion
#region 处[sup]