几个处理字符串的函数(1)

1、UBB代码处理

public static string UBBEncode(string strInput)
            
{
                
if (strInput == null)
                
{
                    
return "";
                }

                
else 
                
{
                    Regex rgxUBB;
                    Match mthUBB;
                    
                    
string str = strInput;
                    str 
= HtmlEncode(str);
                    str 
= SqlEncode(str);
                    str 
= str.Replace(" "," ");
                    str 
= str.Replace("\n","<br />");
                    
                    StringBuilder strbUBB 
= new StringBuilder();
                    strbUBB.Append(str);
    
                    
//处理[b][/b]标记
                    rgxUBB = new Regex(@"(\[b\])([ \S\t]*?)(\[\/b\])",RegexOptions.IgnoreCase); 
                    
for (mthUBB = rgxUBB.Match(strbUBB.ToString()); mthUBB.Success; mthUBB = mthUBB.NextMatch())  
                    

                        strbUBB.Replace(mthUBB.Groups[
0].ToString(),"<B>" + mthUBB.Groups[2].ToString() + "</B>"); 
                    }

    
                    
//处理[i][/i]标记
                    rgxUBB = new Regex(@"(\[i\])([ \S\t]*?)(\[\/i\])",RegexOptions.IgnoreCase); 
                    
for (mthUBB = rgxUBB.Match(strbUBB.ToString()); mthUBB.Success; mthUBB = mthUBB.NextMatch())  
                    

                        strbUBB.Replace(mthUBB.Groups[
0].ToString(),"<I>" + mthUBB.Groups[2].ToString() + "</I>"); 
                    }
 
    
                    
//处理[U][/U]标记
                    rgxUBB = new Regex(@"(\[U\])([ \S\t]*?)(\[\/U\])",RegexOptions.IgnoreCase); 
                    
for (mthUBB = rgxUBB.Match(strbUBB.ToString()); mthUBB.Success; mthUBB = mthUBB.NextMatch())  
                    

                        strbUBB.Replace(mthUBB.Groups[
0].ToString(),"<U>" + mthUBB.Groups[2].ToString() + "</U>"); 
                    }

            
                    
//处理标记
                    rgxUBB = new Regex(@"(\[url\])([ \S\t]*?)(\[\/url\])",RegexOptions.IgnoreCase); 
                    
for (mthUBB = rgxUBB.Match(strbUBB.ToString()); mthUBB.Success; mthUBB = mthUBB.NextMatch())  
                    

                        strbUBB.Replace(mthUBB.Groups[
0].ToString(),"<A href=\"" + mthUBB.Groups[2].ToString() + "\" target=\"_blank\">" + mthUBB.Groups[2].ToString() + "</A>"); 
                    }

    
                    
//处理[url=xxx][/url]标记
                    rgxUBB = new Regex(@"(\[url=([ \S\t]+)\])([ \S\t]*?)(\[\/url\])",RegexOptions.IgnoreCase); 
                    
for (mthUBB = rgxUBB.Match(strbUBB.ToString()); mthUBB.Success; mthUBB = mthUBB.NextMatch())  
                    

                        strbUBB.Replace(mthUBB.Groups[
0].ToString(),"<A href=\"" + mthUBB.Groups[2].ToString() + "\" target=\"_blank\">" + mthUBB.Groups[3].ToString() + "</A>"); 
                    }

                       
                    
//处理[email][/email]标记
                    rgxUBB = new Regex(@"(\[email\])([ \S\t]*?)(\[\/email\])",RegexOptions.IgnoreCase); 
                    
for (mthUBB = rgxUBB.Match(strbUBB.ToString()); mthUBB.Success; mthUBB = mthUBB.NextMatch())  
                    

                        strbUBB.Replace(mthUBB.Groups[
0].ToString(),"<A href=\"mailto:" + mthUBB.Groups[2].ToString() + "\" target=\"_blank\">" + mthUBB.Groups[2].ToString() + "</A>"); 
                    }
 
    
                    
//处理[email=xxx][/email]标记
                    rgxUBB = new Regex(@"(\[email=([ \S\t]+)\])([ \S\t]*?)(\[\/email\])",RegexOptions.IgnoreCase); 
                    
for (mthUBB = rgxUBB.Match(strbUBB.ToString()); mthUBB.Success; mthUBB = mthUBB.NextMatch())  
                    

                        strbUBB.Replace(mthUBB.Groups[
0].ToString(),"<A href=\"mailto:" + mthUBB.Groups[2].ToString() + "\" target=\"_blank\">" + mthUBB.Groups[3].ToString() + "</A>"); 
                    }

    
                    
//处理[center][/center]标记
                    rgxUBB = new Regex(@"(\[center\])([ \S\t]*?)(\[\/center\])",RegexOptions.IgnoreCase); 
                    
for (mthUBB = rgxUBB.Match(strbUBB.ToString()); mthUBB.Success; mthUBB = mthUBB.NextMatch())  
                    

                        strbUBB.Replace(mthUBB.Groups[
0].ToString(),"<div align=center>" + mthUBB.Groups[2].ToString() + "</div>"); 
                    }

    
                    
//处理标记
                    rgxUBB = new Regex(@"(\[img\])([ \S\t]*?)(\[\/img\])",RegexOptions.IgnoreCase); 
                    
for (mthUBB = rgxUBB.Match(strbUBB.ToString()); mthUBB.Success; mthUBB = mthUBB.NextMatch())  
                    

                        strbUBB.Replace(mthUBB.Groups[
0].ToString(),"<img src=" + mthUBB.Groups[2].ToString() + " border=0 />"); 
                    }

    
                    
//处理[flash][/flash]和[flash=x,y][/flash]标记
                    strbUBB = strbUBBEncode(strbUBB,"flash");
    
                    
//处理[mp][/mp]和[mp=x,y][/mp]标记
                    strbUBB = strbUBBEncode(strbUBB,"mp");
    
                    
//处理[rm][/rm]和[rm=x,y][/rm]标记
                    strbUBB = strbUBBEncode(strbUBB,"rm");
    
                    
//处理[code][/code]标记
                    strbUBB = strbUBBEncode(strbUBB,"code");
    
                    
//处理[quote][/quote]标记
                    strbUBB = strbUBBEncode(strbUBB,"quote");
    
                    
//处理[font=xxx][/font]标记
                    rgxUBB = new Regex(@"(\[font=([\S]+)\])([ \S\t]*?)(\[\/font\])",RegexOptions.IgnoreCase); 
                    
for (mthUBB = rgxUBB.Match(strbUBB.ToString()); mthUBB.Success; mthUBB = mthUBB.NextMatch())  
                    

                        strbUBB.Replace(mthUBB.Groups[
0].ToString(),"<FONT FACE=" + mthUBB.Groups[2].ToString() + ">" + mthUBB.Groups[3].ToString() + "</FONT>"); 
                    }

    
                    
//处理[color=xxx][/color]标记
                    rgxUBB = new Regex(@"(\[color=([\S]+)\])([ \S\t]*?)(\[\/color\])",RegexOptions.IgnoreCase); 
                    
for (mthUBB = rgxUBB.Match(strbUBB.ToString()); mthUBB.Success; mthUBB = mthUBB.NextMatch())  
                    

                        strbUBB.Replace(mthUBB.Groups[
0].ToString(),"<FONT COLOR=" + mthUBB.Groups[2].ToString() + ">" + mthUBB.Groups[3].ToString() + "</FONT>"); 
                    }

    
                    
//处理[size=xxx][/size]标记
                    rgxUBB = new Regex(@"(\[size=([1-7])\])([ \S\t]*?)(\[\/size\])",RegexOptions.IgnoreCase); 
                    
for (mthUBB = rgxUBB.Match(strbUBB.ToString()); mthUBB.Success; mthUBB = mthUBB.NextMatch())  
                    

                        strbUBB.Replace(mthUBB.Groups[
0].ToString(),"<FONT SIZE=" + mthUBB.Groups[2].ToString() + ">" + mthUBB.Groups[3].ToString() + "</FONT>"); 
                    }
 
    
                    
return strbUBB.ToString();
                }

            }

            
            
private static StringBuilder strbUBBEncode(StringBuilder strbM_UBB,string strM_Name)
            
{
                
string strM_UBB = strbM_UBB.ToString();
                
if(strM_Name == "flash")
                
{
                    strM_UBB 
= Regex.Replace(strM_UBB,@"\[flash\](?<x>[^\]]*)\[\/flash\]",@"<a href=""$1"" TARGET=_blank>[全屏欣赏]</a><br/><OBJECT align=middle codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=400 height=300><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);
                    strM_UBB 
= Regex.Replace(strM_UBB,@"\[flash=*([0-9]*),*([0-9]*)\](?<x>[^\]]*)\[\/flash\]",@"<a href=""$3"" TARGET=_blank>[全屏欣赏]</a><br/><OBJECT align=middle  codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=$1 height=$2><PARAM NAME=movie VALUE=""$3""><PARAM NAME=quality VALUE=high><embed src=""$3"" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=$1 height=$2>$3</embed></OBJECT>",RegexOptions.IgnoreCase);
                }

                
if(strM_Name == "mp")
                
{
                    strM_UBB 
= Regex.Replace(strM_UBB,@"\[mp\](?<x>[^\]]*)\[\/mp\]",@"<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=400 height=300 ><param name=ShowStatusBar value=-1><param name=Filename value=$1><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=$1 width=400 height=300></embed></object>",RegexOptions.IgnoreCase);
                    strM_UBB 
= Regex.Replace(strM_UBB,@"\[mp=*([0-9]*),*([0-9]*)\](?<x>[^\]]*)\[\/mp\]",@"<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=$1 height=$2 ><param name=ShowStatusBar value=-1><param name=Filename value=$3><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=$4 width=$1 height=$2></embed></object>",RegexOptions.IgnoreCase);
                }

                
if(strM_Name == "rm")
                
{
                    strM_UBB 
= Regex.Replace(strM_UBB,@"\[rm\](?<x>[^\]]*)\[\/rm\]",@"<OBJECT align=middle  classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=400 height=300><PARAM NAME=SRC VALUE=$1><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=true></OBJECT><br/><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=400><PARAM NAME=SRC VALUE=$3><PARAM NAME=AUTOSTART VALUE=-1><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>",RegexOptions.IgnoreCase);
                    strM_UBB 
= Regex.Replace(strM_UBB,@"\[rm=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/rm\]",@"<OBJECT align=middle  classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=$1 height=$2><PARAM NAME=SRC VALUE=$3><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=true></OBJECT><br/><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=$1><PARAM NAME=SRC VALUE=$3><PARAM NAME=AUTOSTART VALUE=-1><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>",RegexOptions.IgnoreCase);
                }

                
if(strM_Name == "code")
                
{
                    strM_UBB 
= Regex.Replace(strM_UBB,@"\[code\](?<x>[^\]]*)\[/code\]",@"—— 以下是代码 ——<br /><table border='1' width='100%' cellpadding='10' cellspacing='0'><tr><td>$1</td></tr></table>",RegexOptions.IgnoreCase); 
                }

                
if(strM_Name == "quote")
                
{
                    strM_UBB 
= Regex.Replace(strM_UBB,@"\[quote](?<x>.*)\[/quote]",@"—— 以下是引用 ——<br /><table border='1' width='100%' cellpadding='10' cellspacing='0'><tr><td>$1</td></tr></table>",RegexOptions.IgnoreCase);
                }

    
                strbM_UBB.Remove(
0,strbM_UBB.Length);
                strbM_UBB.Append(strM_UBB);
                
return strbM_UBB;
            }


Html编码和处理和SQL过滤及其反向处理

 

            public static string SqlEncode(string strInput)
            
{
                
string str;
                
try
                
{
                    str 
= strInput.Replace("'","''");
                }

                
catch
                
{
                    str 
= "error";
                }

                
return str;
            }

            
            
public static string SqlDecode(string strInput)
            
{
                
string str;
                
try
                
{
                    str 
= strInput.Replace("''","'");
                }

                
catch
                
{
                    str 
= "error";
                }

                
return str;
            }

        
            
public static string HtmlEncode(string strInput)
            
{
                
string str;
                
try
                
{
                    str 
= HttpContext.Current.Server.HtmlEncode(strInput);
                }

                
catch
                
{
                    str 
= "error";
                }

                
return str;
            }


            
public static string HtmlDecode(string strInput)
            
{
                
string str;
                
try
                
{
                    str 
= HttpContext.Current.Server.HtmlDecode(strInput);
                }

                
catch
                
{
                    str 
= "error";
                }

                
return str;
            }


MD5和SHA1加密处理

 

            public static string Encrypt(string strInput,string strType)//将字符串加密
            {
                
string str;
                
try
                
{
                    str 
= FormsAuthentication.HashPasswordForStoringInConfigFile(strInput,strType);
                }

                
catch
                
{
                    str 
= "error";
                }

                
return str;
            }

        
            
public static string GetMD5(string strInput)//将字符串用MD5加密
            {
                
return Encrypt(strInput,"MD5");
            }

            
            
public static string GetSHA1(string strInput)//将字符串用SHA1加密
            {
                
return Encrypt(strInput,"SHA1");
            }


字符串截取

 

            public static string CutString(string strInput,int intlen)//截取字符串
            {
                ASCIIEncoding ascii 
=  new ASCIIEncoding();
                
int intLength = 0;
                
string strString = "";
                
byte[] s = ascii.GetBytes(strInput);
                
for(int i = 0;i < s.Length;i++)
                
{
                    
if((int)s[i] == 63)
                    
{
                        intLength 
+= 2;
                    }

                    
else
                    
{
                        intLength 
+= 1;
                    }

                    
                    
try
                    
{
                        strString 
+= strInput.Substring(i,1);
                    }

                    
catch
                    
{
                        
break;
                    }

    
                    
if(intLength > intlen)
                    
{
                        
break;
                    }

                }

                
//如果截过则加上半个省略号
                byte[] mybyte=System.Text.Encoding.Default.GetBytes(strInput);
                
if(mybyte.Length > intlen)
                
{
                    strString 
+= "";
                }

                
return strString;
            }


简单的几个字符串处理函数,可以直接调用,比较方便。

批评建议请发Email给我:kenblove#gmail.com

posted @ 2006-08-01 09:20  KenBlove  阅读(366)  评论(1编辑  收藏  举报