程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> UBB(C#)

UBB(C#)

編輯:關於C語言
 參考了一些文章,整理了一下,大家可以直接拿去用吧,其實自從有了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(" "," ");
   #endregion
   #region Html標記符
   sDetail = sDetail.Replace("<","<");
   sDetail = sDetail.Replace(">",">");
   #endregion
   #region 處標記
   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 處標記
   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 處標記
   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 處標記
   //處標記
   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 處標記
   //處標記
   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 處標記
   //處標記
   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 處標記
   //處標記
   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 處標記
   //處標記
   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 處標記
   //處標記
   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 處理

   //處理

   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(( )| )+)(?<正文>\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
 }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved