首页 > 开发 > 综合 > 正文

UBB(C#)

2024-07-21 02:27:24
字体:
来源:转载
供稿:网友

  参考了一些文章,整理了一下,大家可以直接拿去用吧,其实自从有了freetextbox这样的东东出现,ubb已经渐渐淡出江湖了。
using system;
using system.text;
using system.text.regularexpressions;
namespace test.com
{
/// <summary>
 /// 功能:ubb代码
 /// 作者:rexsp
 /// 日期:2004-4-6
 /// </summary>
 public class ubb
 {
  #region 构造函数
  public ubb()
  {
   //
   // todo: 在此处添加构造函数逻辑
   //
  }
  #endregion
  #region 公共静态方法
  /// <summary>
  /// ubb代码处理函数
  /// </summary>
  /// <param name="sdetail">输入字符串</param>
  /// <returns>输出字符串</returns>
  public static string ubbtohtml(string sdetail)
  {
   regex r;
   match m;
   #region 处理空格
   sdetail = sdetail.replace(" ","&nbsp;");
   #endregion
   #region html标记符
   sdetail = sdetail.replace("<","&lt;");
   sdetail = sdetail.replace(">","&gt;");
   #endregion
   #region 处[b][/b]标记
   r = new regex(@"(/[b/])([ /s/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 处[i][/i]标记
   r = new regex(@"(/[i/])([ /s/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 处[u][/u]标记
   r = new regex(@"(/[u/])([ /s/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][/sup]标记
   //处[sup][/sup]标记
   r = new regex(@"(/[sup/])([ /s/t]*?)(/[//sup/])",regexoptions.ignorecase);
   for (m = r.match(sdetail); m.success; m = m.nextmatch())
   {
    sdetail = sdetail.replace(m.groups[0].tostring(),"<sup>" + m.groups[2].tostring() + "</sup>");
   }
   #endregion
   #region 处[sub][/sub]标记
   //处[sub][/sub]标记
   r = new regex(@"(/[sub/])([ /s/t]*?)(/[//sub/])",regexoptions.ignorecase);
   for (m = r.match(sdetail); m.success; m = m.nextmatch())
   {
    sdetail = sdetail.replace(m.groups[0].tostring(),"<sub>" + m.groups[2].tostring() + "</sub>");
   }
   #endregion
   #region 处[url][/url]标记
   //处[url][/url]标记
   r = new regex(@"(/[url/])([ /s/t]*?)(/[//url/])",regexoptions.ignorecase);
   for (m = r.match(sdetail); m.success; m = m.nextmatch())
   {
    sdetail = sdetail.replace(m.groups[0].tostring(),
     "<a href=/"" + m.groups[2].tostring() + "/" target=/"_blank/"><img border=0 src=/"images/url.gif/">" +
     m.groups[2].tostring() + "</a>");
   }
   #endregion
   #region 处[url=xxx][/url]标记
   //处[url=xxx][/url]标记
   r = new regex(@"(/[url=([ /s/t]+)/])([ /s/t]*?)(/[//url/])",regexoptions.ignorecase);
   for (m = r.match(sdetail); m.success; m = m.nextmatch())
   {
    sdetail = sdetail.replace(m.groups[0].tostring(),
     "<a href=/"" + m.groups[2].tostring() + "/" target=/"_blank/"><img border=0 src=/"images/url.gif/">" +
     m.groups[3].tostring() + "</a>");
   }
   #endregion
   #region 处[email][/email]标记
   //处[email][/email]标记
   r = new regex(@"(/[email/])([ /s/t]*?)(/[//email/])",regexoptions.ignorecase);
   for (m = r.match(sdetail); m.success; m = m.nextmatch())
   {
    sdetail = sdetail.replace(m.groups[0].tostring(),
     "<a href=/"mailto:" + m.groups[2].tostring() + "/" target=/"_blank/"><img border=0 src=/"images/email.gif/">" +
     m.groups[2].tostring() + "</a>");
   }
   #endregion
   #region 处[email=xxx][/email]标记
   //处[email=xxx][/email]标记
   r = new regex(@"(/[email=([ /s/t]+)/])([ /s/t]*?)(/[//email/])",regexoptions.ignorecase);
   for (m = r.match(sdetail); m.success; m = m.nextmatch())
   {
    sdetail = sdetail.replace(m.groups[0].tostring(),
     "<a href=/"mailto:" + m.groups[2].tostring() + "/" target=/"_blank/"><img border=0 src=/"images/email.gif/">" +
     m.groups[3].tostring() + "</a>");
   }
   #endregion
   #region 处[size=x][/size]标记
   //处[size=x][/size]标记
   r = new regex(@"(/[size=([1-7])/])([ /s/t]*?)(/[//size/])",regexoptions.ignorecase);
   for (m = r.match(sdetail); m.success; m = m.nextmatch())
   {
    sdetail = sdetail.replace(m.groups[0].tostring(),
     "<font size=" + m.groups[2].tostring() + ">" +
     m.groups[3].tostring() + "</font>");
   }
   #endregion
   #region 处[color=x][/color]标记
   //处[color=x][/color]标记
   r = new regex(@"(/[color=([/s]+)/])([ /s/t]*?)(/[//color/])",regexoptions.ignorecase);
   for (m = r.match(sdetail); m.success; m = m.nextmatch())
   {
    sdetail = sdetail.replace(m.groups[0].tostring(),
     "<font color=" + m.groups[2].tostring() + ">" +
     m.groups[3].tostring() + "</font>");
   }
   #endregion
   #region 处[font=x][/font]标记
   //处[font=x][/font]标记
   r = new regex(@"(/[font=([/s]+)/])([ /s/t]*?)(/[//font/])",regexoptions.ignorecase);
   for (m = r.match(sdetail); m.success; m = m.nextmatch())
   {
    sdetail = sdetail.replace(m.groups[0].tostring(),
     "<font face=" + m.groups[2].tostring() + ">" +
     m.groups[3].tostring() + "</font>");
   }
   #endregion
   #region 处理图片链接
   //处理图片链接
   r = new regex("//[picture//](//d+?)//[///picture//]",regexoptions.ignorecase);
   for (m = r.match(sdetail); m.success; m = m.nextmatch())
   {
    sdetail = sdetail.replace(m.groups[0].tostring(),
     "<a href=/"showimage.aspx?type=all&action=forumimage&imageid=" + m.groups[1].tostring() +
     "/" target=/"_blank/"><img border=0 title=/"点击打开新窗口查看/" src=/"showimage.aspx?action=forumimage&imageid=" + m.groups[1].tostring() +
     "/"></a>");
   }
   #endregion
   #region 处理[align=x][/align]
   //处理[align=x][/align]
   r = new regex(@"(/[align=([/s]+)/])([ /s/t]*?)(/[//align/])",regexoptions.ignorecase);
   for (m = r.match(sdetail); m.success; m = m.nextmatch())
   {
    sdetail = sdetail.replace(m.groups[0].tostring(),
     "<p align=" + m.groups[2].tostring() + ">" +
     m.groups[3].tostring() + "</p>");
   }
   #endregion
   #region 处[h=x][/h]标记
   //处[h=x][/h]标记
   r = new regex(@"(/[h=([1-6])/])([ /s/t]*?)(/[//h/])",regexoptions.ignorecase);
   for (m = r.match(sdetail); m.success; m = m.nextmatch())
   {
    sdetail = sdetail.replace(m.groups[0].tostring(),
     "<h" + m.groups[2].tostring() + ">" +
     m.groups[3].tostring() + "</h" + m.groups[2].tostring() + ">");
   }
   #endregion
   #region 处理[list=x][*][/list]
   //处理[list=x][*][/list]
   r = new regex(@"(/[list(=(a|a|i|i| ))?/]([ /s/t]*)/r/n)((/[/*/]([ /s/t]*/r/n))*?)(/[//list/])",regexoptions.ignorecase);
   for (m = r.match(sdetail); m.success; m = m.nextmatch())
   {
    string strli = m.groups[5].tostring();
    regex rli = new regex(@"/[/*/]([ /s/t]*/r/n?)",regexoptions.ignorecase);
    match mli;
    for (mli = rli.match(strli); mli.success; mli = mli.nextmatch())
    {
     strli = strli.replace(mli.groups[0].tostring(),"<li>" + mli.groups[1]);
    }
    sdetail = sdetail.replace(m.groups[0].tostring(),
     "<ul type=/"" + m.groups[3].tostring() + "/"><b>" + m.groups[4].tostring() + "</b>" +
     strli + "</ul>");
   }
   #endregion
   #region 处理换行
   //处理换行,在每个新行的前面添加两个全角空格
   r = new regex(@"(/r/n((&nbsp;)| )+)(?<正文>/s+)",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
   return sdetail;
  }
  #endregion
 }
}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表