程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#中HTML和UBB互相轉換的代碼,

C#中HTML和UBB互相轉換的代碼,

編輯:C#入門知識

C#中HTML和UBB互相轉換的代碼,


C#中HTML和UBB互相轉換的代碼
html轉UBB的還不是很完美,有空修改,一些代碼來自百度谷歌

private string DoHtmlToUBB(string _Html)
        {
 
            _Html = Regex.Replace(_Html, "<br[^>]*>", "\n");
            _Html = Regex.Replace(_Html, @"<p[^>\/]*\/>", "\n");
            _Html = Regex.Replace(_Html, "\\son[\\w]{3,16}\\s?=\\s*([\'\"]).+?\\1", "");
            _Html = Regex.Replace(_Html, "<hr[^>]*>", "[hr]");
 
            _Html = Regex.Replace(_Html, "<(\\/)?blockquote([^>]*)>", "[$1blockquote]");
            _Html = Regex.Replace(_Html, "<img[^>]*smile=\"(\\d+)\"[^>]*>", "'[s:$1]");
            _Html = Regex.Replace(_Html, "<img[^>]*src=[\'\"\\s]*([^\\s\'\"]+)[^>]*>", "");
            _Html = Regex.Replace(_Html, "<a[^>]*href=[\'\"\\s]*([^\\s\'\"]*)[^>]*>(.+?)<\\/a>", "[url=$1]$2[/url]");
            _Html = Regex.Replace(_Html, "<b>(.+?)</b>",@"\[b\]$1\[/b\]");
            _Html = Regex.Replace(_Html, "<[^>]*?>", "");
            _Html = Regex.Replace(_Html, "&amp;", "&");
            _Html = Regex.Replace(_Html, "&nbsp;", " ");
            _Html = Regex.Replace(_Html, "&lt;", "<");
            _Html = Regex.Replace(_Html, "&gt;", ">");
 
            return _Html;
        }
        public string ubbtohtml(string content)  //ubb轉html
        {
            content = Regex.Replace(content, @"\r\n", "<br/>");
            content = Regex.Replace(content, " ", "&nbsp;");
            content = Regex.Replace(content, @"\[b\](.+?)\[/b\]", "<b>$1</b>");
            content = Regex.Replace(content, @"\[i\](.+?)\[/i\]", "<i>$1</i>");
            content = Regex.Replace(content, @"\[u\](.+?)\[/u\]", "<u>$1</u>");
            content = Regex.Replace(content, @"\[p\](.+?)\[/p\]", "<p class='load'>$1</p>");
            content = Regex.Replace(content, @"\[align=left\](.+?)\[/align\]", "<align='left'>$1</align>");
            content = Regex.Replace(content, @"\[align=center\](.+?)\[/align\]", "<align='center'>$1</align>");
            content = Regex.Replace(content, @"\[align=right\](.+?)\[/align\]", "<align='right'>$1</align>");
            content = Regex.Replace(content, @"\[url=(?<url>.+?)]\[/url]", "<a href='${url}' target=_blank>${url}</a>");
            content = Regex.Replace(content, @"\[url=(?<url>.+?)](?<name>.+?)\[/url]", "<a href='${url}' target=_blank>${name}</a>");
            content = Regex.Replace(content, @"\[quote](?<text>.+?)\[/quote]", "<div class="quote">${text}</div>");
            content = Regex.Replace(content, @"\[img](?<img>.+?)\[/img]", "<img src='${img}' alt=''/>");
            return content;
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved