濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > C#正則實(shí)現(xiàn)Ubb解析類的代碼

C#正則實(shí)現(xiàn)Ubb解析類的代碼

熱門標(biāo)簽:知名的電話機(jī)器人 湖南企業(yè)智能外呼系統(tǒng)供應(yīng)商 RO地圖標(biāo)注app 錫林郭勒盟地圖標(biāo)注位置 高德地圖標(biāo)注短信簽約 百音電話機(jī)器人 福州工作銷售電話機(jī)器人 電銷機(jī)器人公司簡(jiǎn)介 自制電銷機(jī)器人
解析得到的代碼能通過(guò)XHTML 1.0 STRICT驗(yàn)證;
包含了標(biāo)題,鏈接,字體,對(duì)齊,圖片,引用,列表等方面的功能. 
Ubb.ReadMe.htm

[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]

復(fù)制代碼 代碼如下:

//作者:deerchao 
// http://www.unibetter.com/blogs/blogdeerchao/default.aspx 
//在不移除以上(及本條)注釋的前提下,任何人可以以任何方式使用此代碼. 

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Web; 
using System.Text.RegularExpressions; 

namespace Deerchao.Web 

    public class UbbDecoder 
    { 
        private static readonly RegexOptions options = RegexOptions.Compiled | RegexOptions.Singleline; 

        /// summary> 
        /// 解析Ubb代碼為Html代碼 
        /// /summary> 
        /// param name="ubb">Ubb代碼/param> 
        /// returns>解析得到的Html代碼/returns> 
        public static string Decode(string ubb) 
        { 
            if (string.IsNullOrEmpty(ubb)) 
                return null; 
            string result = ubb; 
            result = HttpUtility.HtmlEncode(result); 

            result = DecodeStyle(result); 
            result = DecodeFont(result); 
            result = DecodeColor(result); 
            result = DecodeImage(result); 
            result = DecodeLinks(result); 
            result = DecodeQuote(result); 
            result = DecodeAlign(result); 
            result = DecodeList(result); 
            result = DecodeHeading(result); 
            result = DecodeBlank(result); 

            return result; 
        } 

        /// summary> 
        /// 解析Ubb代碼為Html代碼,所有的鏈接為rel="nofollow" 
        /// /summary> 
        /// param name="ubb">Ubb代碼/param> 
        /// returns>解析得到的Html代碼/returns> 
        public static string DecodeNoFollow(string ubb) 
        { 
            if (string.IsNullOrEmpty(ubb)) 
                return null; 
            string result = ubb; 
            result = HttpUtility.HtmlEncode(result); 

            result = DecodeStyle(result); 
            result = DecodeFont(result); 
            result = DecodeColor(result); 
            result = DecodeImage(result); 
            result = DecodeLinksNoFollow(result); 
            result = DecodeQuote(result); 
            result = DecodeAlign(result); 
            result = DecodeList(result); 
            result = DecodeHeading(result); 
            result = DecodeBlank(result); 

            return result; 
        } 

        private static string DecodeHeading(string ubb) 
        { 
            string result = ubb; 
            result = Regex.Replace(result, @"\[h(\d)\](.*?)\[/h\1\]", "h$1>$2/h$1>", options); 
            return result; 
        } 

        private static string DecodeList(string ubb) 
        { 
            string sListFormat = "ol style=\"list-style:{0};\">$1/ol>"; 
            string result = ubb; 
            // Lists 
            result = Regex.Replace(result, @"\[\*\]([^\[]*)", "li>$1/li>", options); 
            result = Regex.Replace(result, @"\[list\]\s*(.*?)\[/list\]", "ul>$1/ul>", options); 
            result = Regex.Replace(result, @"\[list=1\]\s*(.*?)\[/list\]", string.Format(sListFormat, "decimal"), options); 
            result = Regex.Replace(result, @"\[list=i\]\s*(.*?)\[/list\]", string.Format(sListFormat, "lower-roman"), options); 
            result = Regex.Replace(result, @"\[list=I\]\s*(.*?)\[/list\]", string.Format(sListFormat, "upper-roman"), options); 
            result = Regex.Replace(result, @"\[list=a\]\s*(.*?)\[/list\]", string.Format(sListFormat, "lower-alpha"), options); 
            result = Regex.Replace(result, @"\[list=A\]\s*(.*?)\[/list\]", string.Format(sListFormat, "upper-alpha"), options); 

            return result; 
        } 

        private static string DecodeBlank(string ubb) 
        { 
            string result = ubb; 

            result = Regex.Replace(result, @"(?= ) | (?= )", "nbsp;", options); 
            result = Regex.Replace(result, @"\r\n", "br />"); 
            string[] blockTags = {"h[1-6]", "li", "list", "div", "p", "ul"}; 
            //clear br before block tags(start or end) 
            foreach (string tag in blockTags) 
            { 
                Regex r = new Regex("br />(" + tag + ")",options); 
                result = r.Replace(result, "$1"); 
                r = new Regex("br />(/" + tag + ")",options); 
                result = r.Replace(result, "$1"); 
            } 
            return result; 
        } 

        private static string DecodeAlign(string ubb) 
        { 
            string result = ubb; 

            result = Regex.Replace(result, @"\[left\](.*?)\[/left\]", "div style=\"text-align:left\">$1/div>", options); 
            result = Regex.Replace(result, @"\[right\](.*?)\[/right\]", "div style=\"text-align:right\">$1/div>", options); 
            result = Regex.Replace(result, @"\[center\](.*?)\[/center\]", "div style=\"text-align:center\">$1/div>", options); 

            return result; 
        } 

        private static string DecodeQuote(string ubb) 
        { 
            string result = ubb; 

            result = Regex.Replace(result, @"\[quote\]", "blockquote>div>", options); 
            result = Regex.Replace(result, @"\[/quote\]", "/div>/blockquote>", options); 
            return result; 
        } 

        private static string DecodeFont(string ubb) 
        { 
            string result = ubb; 

            result = Regex.Replace(result, @"\[size=([-\w]+)\](.*?)\[/size\]", "span style=\"font-size:$1\">$2/span>", options); 
            result = Regex.Replace(result, @"\[font=(.*?)\](.*?)\[/font\]", "span style=\"font-family:$1\">$2/span>", options); 
            return result; 
        } 

        private static string DecodeLinks(string ubb) 
        { 
            string result = ubb; 

            result = Regex.Replace(result, @"\[url\]www\.(.*?)\[/url\]", "a href=\"http://www.$1\">$1/a>", options); 
            result = Regex.Replace(result, @"\[url\](.*?)\[/url\]", "a href=\"$1\">$1/a>", options); 
            result = Regex.Replace(result, @"\[url=(.*?)\](.*?)\[/url\]", "a href=\"$1\" title=\"$2\">$2/a>", options); 
            result = Regex.Replace(result, @"\[email\](.*?)\[/email\]", "a href=\"mailto:$1\">$1/a>", options); 
            return result; 
        } 

        private static string DecodeLinksNoFollow(string ubb) 
        { 
            string result = ubb; 

            result = Regex.Replace(result, @"\[url\]www\.(.*?)\[/url\]", "a rel=\"nofollow\" href=\"http://www.$1\">$1/a>", options); 
            result = Regex.Replace(result, @"\[url\](.*?)\[/url\]", "a rel=\"nofollow\" href=\"$1\">$1/a>", options); 
            result = Regex.Replace(result, @"\[url=(.*?)\](.*?)\[/url\]", "a rel=\"nofollow\" href=\"$1\" title=\"$2\">$2/a>", options); 
            result = Regex.Replace(result, @"\[email\](.*?)\[/email\]", "a href=\"mailto:$1\">$1/a>", options); 
            return result; 
        } 

        private static string DecodeImage(string ubb) 
        { 
            string result = ubb; 

            result = Regex.Replace(result, @"\[hr\]", "hr />", options); 
            result = Regex.Replace(result, @"\[img\](.+?)\[/img\]", "img src=\"$1\" alt=\"\" />", options); 
            result = Regex.Replace(result, @"\[img=(\d+)x(\d+)\](.+?)\[/img\]", "img src=\"$3\" style=\"width:$1px;height:$2px\" alt=\"\" />", options); 

            return result; 
        } 

        private static string DecodeColor(string ubb) 
        { 
            string result = ubb; 
            result = Regex.Replace(result, @"\[color=(#?\w+?)\](.+?)\[/color\]", "span style=\"color:$1\">$2/span>",options); 

            return result; 
        } 

        private static string DecodeStyle(string ubb) 
        { 
            string result=ubb; 
            //we don't need this for perfomance and other consideration: 
            //(table[^>]*>(?>table[^>]*>(?Depth>)|/table>(?-Depth>)|.)+(?(Depth)(?!))/table>) 
            result = Regex.Replace(result, @"\[[b]\](.*?)\[/[b]\]", "strong>$1/strong>", options); 
            result = Regex.Replace(result, @"\[[u]\](.*?)\[/[u]\]", "span style=\"text-decoration:underline\">$1/span>", options); 
            result = Regex.Replace(result, @"\[[i]\](.*?)\[/[i]\]", "i>$1/i>", options); 

            return result; 
        } 
    } 

您可能感興趣的文章:
  • xml 封裝與解析(javascript和C#中)
  • C#解析json文件的實(shí)現(xiàn)代碼
  • C#下解析HTML的兩種方法介紹
  • asp.net C#生成和解析二維碼的實(shí)例代碼
  • c#實(shí)現(xiàn)flv解析詳解示例
  • c#中XML解析文件出錯(cuò)解決方法
  • C#解析JSON實(shí)例
  • C# 解析 Excel 并且生成 Csv 文件代碼分析
  • 用C#來(lái)解析PDF文件
  • C#解析Lrc歌詞文件過(guò)程詳解

標(biāo)簽:吉林 玉林 河北 怒江 西寧 茂名 秦皇島 昆明

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《C#正則實(shí)現(xiàn)Ubb解析類的代碼》,本文關(guān)鍵詞  正則,實(shí)現(xiàn),Ubb,解析,類,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《C#正則實(shí)現(xiàn)Ubb解析類的代碼》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于C#正則實(shí)現(xiàn)Ubb解析類的代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    怀化市| 尉犁县| 乐平市| 石景山区| 廊坊市| 全南县| 从江县| 铜川市| 托克逊县| 久治县| 清苑县| 两当县| 博乐市| 丰宁| 皋兰县| 乳山市| 南宫市| 扎鲁特旗| 綦江县| 栾城县| 沾益县| 昌邑市| 武隆县| 都江堰市| 尚义县| 静安区| 高碑店市| 商丘市| 柳江县| 五河县| 青州市| 平塘县| 涪陵区| 尉犁县| 潮安县| 临安市| 岢岚县| 阿坝县| 观塘区| 集安市| 永宁县|