本文实例讲述了C#实现HTML和UBB互相转换的方法。分享给大家供大家参考。具体实现方法如下:
html转UBB的还不是很完美,有空修改,一些代码来自百度谷歌,如下所示:
{
_Html = Regex.Replace(_Html, "
]*>", "\n");
_Html = Regex.Replace(_Html, @"
\/]*\/>", "\n");
_Html = Regex.Replace(_Html, "\\son[\\w]{3,16}\\s?=\\s*([\'\"]).+?\\1", "");
_Html = Regex.Replace(_Html, "
]*>", "[hr]");
_Html = Regex.Replace(_Html, "<(\\/)?blockquote([^>]*)>", "[$1blockquote]");
_Html = Regex.Replace(_Html, "]*smile=\"(\\d+)\"[^>]*>", "'[s:$1]");
_Html = Regex.Replace(_Html, "]*src=[\'\"\\s]*([^\\s\'\"]+)[^>]*>", "");
_Html = Regex.Replace(_Html, "]*href=[\'\"\\s]*([^\\s\'\"]*)[^>]*>(.+?)<\\/a>", "[url=$1]$2[/url]");
_Html = Regex.Replace(_Html, "(.+?)",@"\[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", "
");
content = Regex.Replace(content, " ", " ");
content = Regex.Replace(content, @"\[b\](.+?)\[/b\]", "$1");
content = Regex.Replace(content, @"\[i\](.+?)\[/i\]", "$1");
content = Regex.Replace(content, @"\[u\](.+?)\[/u\]", "$1");
content = Regex.Replace(content, @"\[p\](.+?)\[/p\]", "
$1
");content = Regex.Replace(content, @"\[align=left\](.+?)\[/align\]", "
content = Regex.Replace(content, @"\[align=center\](.+?)\[/align\]", "
content = Regex.Replace(content, @"\[align=right\](.+?)\[/align\]", "
content = Regex.Replace(content, @"\[url=(?
content = Regex.Replace(content, @"\[url=(?
content = Regex.Replace(content, @"\[quote](?
content = Regex.Replace(content, @"\[img](?.+?)\[/img]", "");
return content;
}
希望本文所述对大家的C#程序设计有所帮助。