UBB 转 HTML C#代码

 

  using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Text;
using  System.Text.RegularExpressions;

public   partial   class  UBB : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        

    }



    
公共静态方法
    
protected void btn_OK_Click(object sender, EventArgs e)
    
{
        
//string S = "";
        txtResult.Text= UBBToHTML(txtUBB.Text);
    }

}


 

 

在网上找的更简单的写法

public   string  Texttohtml( string  chr)
  
{
  
   
if(chr==null)
    
return "";
   chr
=chr.Replace("<","&lt");
   chr
=chr.Replace(">","&gt");
   chr
=chr.Replace(" ","<br/>");
   chr 
= Regex.Replace(chr,@"[url=(?<x>[^]]*)](?<y>[^]]*)[/url]",@"<a href=$1 target=_blank>$2</a>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[url](?<x>[^]]*)[/url]",@"<a href=$1 target=_blank>$1</a>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[email=(?<x>[^]]*)](?<y>[^]]*)[/email]",@"<a href=$1>$2</a>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[email](?<x>[^]]*)[/email]",@"<a href=$1>$1</a>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[flash](?<x>[^]]*)[/flash]",@"<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=500 height=400><PARAM NAME=movie VALUE=""$1""><PARAM NAME=quality VALUE=high><embed src=""$1"" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=500 height=400>$1</embed></OBJECT>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[img](?<x>[^]]*)[/img]",@"<IMG SRC=""$1"" border=0>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[color=(?<x>[^]]*)](?<y>[^]]*)[/color]",@"<font color=$1>$2</font>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[face=(?<x>[^]]*)](?<y>[^]]*)[/face]",@"<font face=$1>$2</font>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[size=1](?<x>[^]]*)[/size]",@"<font size=1>$1</font>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[size=2](?<x>[^]]*)[/size]",@"<font size=2>$1</font>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[size=3](?<x>[^]]*)[/size]",@"<font size=3>$1</font>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[size=4](?<x>[^]]*)[/size]",@"<font size=4>$1</font>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[align=(?<x>[^]]*)](?<y>[^]]*)[/align]",@"<align=$1>$2</align>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[fly](?<x>[^]]*)[/fly]",@"<marquee width=90% behavior=alternate scrollamount=3>$1</marquee>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[move](?<x>[^]]*)[/move]",@"<marquee scrollamount=3>$1</marquee>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[glow=(?<x>[^]]*),(?<y>[^]]*),(?<z>[^]]*)](?<w>[^]]*)[/glow]",@"<table width=$1 style='filter:glow(color=$2, strength=$3)'>$4</table>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[shadow=(?<x>[^]]*),(?<y>[^]]*),(?<z>[^]]*)](?<w>[^]]*)[/shadow]",@"<table width=$1 style='filter:shadow(color=$2, strength=$3)'>$4</table>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[b](?<x>[^]]*)[/b]",@"<b>$1</b>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[i](?<x>[^]]*)[/i]",@"<i>$1</i>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[u](?<x>[^]]*)[/u]",@"<u>$1</u>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[code](?<x>[^]]*)[/code]",@"<pre id=code><font size=1 face='Verdana, Arial' id=code>$1</font id=code></pre id=code>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[list](?<x>[^]]*)[/list]",@"<ul>$1</ul>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[list=1](?<x>[^]]*)[/list]",@"<ol type=1>$1</ol id=1>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[list=a](?<x>[^]]*)[/list]",@"<ol type=a>$1</ol id=a>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[*](?<x>[^]]*)[/*]",@"<li>$1</li>",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"[quote](?<x>.*)[/quote]",@"<center>—— 以下是引用 ——<table border='1' width='80%' cellpadding='10' cellspacing='0' ><tr><td>$1</td></tr></table></center>",RegexOptions.IgnoreCase);
   
return(chr); 
  }

  
public   string  htmltoText( string  chr)
  
{
   
if(chr==null)
    
return "";
   chr
=chr.Replace("'","''");
   
return(chr); 
  }

 

 

html转UBB代码,还没有测试

 

public   string  TexttoUBB( string  chr)
{
  
   
if(chr==null)
    
return "";
   chr
=chr.Replace("&lt","<");
   chr
=chr.Replace("&gt",">");
   chr
=chr.Replace("<br/>"," ");
   chr 
= Regex.Replace(chr,@"<a href=$1 target=_blank>$2</a>",@"[url=(?<x>[^]]*)](?<y>[^]]*)[/url]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<a href=$1 target=_blank>$1</a>",@"[url](?<x>[^]]*)[/url]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<a href=$1>$2</a>",@"[email=(?<x>[^]]*)](?<y>[^]]*)[/email]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<a href=$1>$1</a>",@"[email](?<x>[^]]*)[/email]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=500 height=400><PARAM NAME=movie VALUE=""$1""><PARAM NAME=quality VALUE=high><embed src=""$1"" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=500 height=400>$1</embed></OBJECT>",@"[flash](?<x>[^]]*)[/flash]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<IMG SRC=""$1"" border=0>",@"[img](?<x>[^]]*)[/img]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<font color=$1>$2</font>",@"[color=(?<x>[^]]*)](?<y>[^]]*)[/color]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<font face=$1>$2</font>",@"[face=(?<x>[^]]*)](?<y>[^]]*)[/face]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<font size=1>$1</font>",@"[size=1](?<x>[^]]*)[/size]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<font size=2>$1</font>",@"[size=2](?<x>[^]]*)[/size]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<font size=3>$1</font>",@"[size=3](?<x>[^]]*)[/size]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<font size=4>$1</font>",@"[size=4](?<x>[^]]*)[/size]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<align=$1>$2</align>",@"[align=(?<x>[^]]*)](?<y>[^]]*)[/align]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<marquee width=90% behavior=alternate scrollamount=3>$1</marquee>",@"[fly](?<x>[^]]*)[/fly]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<marquee scrollamount=3>$1</marquee>",@"[move](?<x>[^]]*)[/move]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<table width=$1 style='filter:glow(color=$2, strength=$3)'>$4</table>",@"[glow=(?<x>[^]]*),(?<y>[^]]*),(?<z>[^]]*)](?<w>[^]]*)[/glow]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<table width=$1 style='filter:shadow(color=$2, strength=$3)'>$4</table>",@"[shadow=(?<x>[^]]*),(?<y>[^]]*),(?<z>[^]]*)](?<w>[^]]*)[/shadow]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<b>$1</b>",@"[b](?<x>[^]]*)[/b]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<i>$1</i>",@"[i](?<x>[^]]*)[/i]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<u>$1</u>",@"[u](?<x>[^]]*)[/u]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<pre id=code><font size=1 face='Verdana, Arial' id=code>$1</font id=code></pre id=code>",@"[code](?<x>[^]]*)[/code]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<ul>$1</ul>",@"[list](?<x>[^]]*)[/list]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<ol type=1>$1</ol id=1>",@"[list=1](?<x>[^]]*)[/list]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<ol type=a>$1</ol id=a>",@"[list=a](?<x>[^]]*)[/list]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<li>$1</li>",@"[*](?<x>[^]]*)[/*]",RegexOptions.IgnoreCase);
   chr 
= Regex.Replace(chr,@"<center>—— 以下是引用 ——<table border='1' width='80%' cellpadding='10' cellspacing='0' ><tr><td>$1</td></tr></table></center>",@"[quote](?<x>.*)[/quote]",RegexOptions.IgnoreCase);
   
return(chr); 
  }

你可能感兴趣的:(UBB 转 HTML C#代码)