C#实现HTML和UBB互相转换的方法
本文实例讲述了c#实现html和ubb互相转换的方法。分享给大家供大家参考。具体实现方法如下:
html转ubb的还不是很完美,有空修改,一些代码来自百度谷歌,如下所示:
{
_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, "&", "&");
_html = regex.replace(_html, " ", " ");
_html = regex.replace(_html, "<", "<");
_html = regex.replace(_html, ">", ">");
return _html;
}
public string ubbtohtml(string content) //ubb转html
{
content = regex.replace(content, @"\r\n", "<br/>");
content = regex.replace(content, " ", " ");
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;
}
希望本文所述对大家的c#程序设计有所帮助。