C#实现HTML和UBB互相转换的方法
本文实例讲述了C#实现HTML和UBB互相转换的方法。分享给大家供大家参考。具体实现方法如下:
html转UBB的还不是很完美,有空修改,一些代码来自百度谷歌,如下所示:
privatestringDoHtmlToUBB(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,"&","&"); _Html=Regex.Replace(_Html," ",""); _Html=Regex.Replace(_Html,"<","<"); _Html=Regex.Replace(_Html,">",">");
return_Html; } publicstringubbtohtml(stringcontent) //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\]","<pclass='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]","<ahref='${url}'target=_blank>${url}</a>"); content=Regex.Replace(content,@"\[url=(?<url>.+?)](?<name>.+?)\[/url]","<ahref='${url}'target=_blank>${name}</a>"); content=Regex.Replace(content,@"\[quote](?<text>.+?)\[/quote]","<divclass="quote">${text}</div>"); content=Regex.Replace(content,@"\[img](?<img>.+?)\[/img]","<imgsrc='${img}'alt=''/>"); returncontent; }