将用户输入的字符串转换为可换行、替换Html编码、无危害数据库特殊字符、去掉首尾空白、的安全方便代码
Posted on 2006-08-01 10:26 小鱼儿 阅读(1390) 评论(2) 编辑 收藏 举报//正则表达式 命名空间引用
using System.Text.RegularExpressions;
/// <summary>
/// 将用户输入的字符串转换为可换行、替换Html编码、无危害数据库特殊字符、去掉首尾空白、的安全方便代码。
/// </summary>
/// <param name="inputString">用户输入字符串</param>
public static string ConvertStr(string inputString)
{
string retVal=inputString;
retVal=retVal.Replace("&","&");
retVal=retVal.Replace("\"",""");
retVal=retVal.Replace("<","<");
retVal=retVal.Replace(">",">");
retVal=retVal.Replace(" "," ");
retVal=retVal.Replace(" "," ");
retVal=retVal.Replace("\t"," ");
retVal=retVal.Replace("\r", "<br>");
return retVal;
}
public static string ToUrl(string inputString)
{
string retVal=inputString;
retVal= ConvertStr(retVal);
retVal= Regex.Replace(retVal,@"\[url](?<x>[^\]]*)\[/url]",@"<a href=""$1"" target=""_blank"">$1</a>",RegexOptions.IgnoreCase);
retVal= Regex.Replace(retVal,@"\[flash=(?<width>\d+),(?<height>\d+)](?<x>[^\]]*)\[/flash]",@"<embed src=""$3"" width=""${width}"" height=""${height}""></embed>",RegexOptions.IgnoreCase);
retVal= Regex.Replace(retVal,@"\[flash](?<x>[^\]]*)\[/flash]",@"<embed src=""$1""></embed>",RegexOptions.IgnoreCase);
return Regex.Replace(retVal,@"\[img](?<x>[^\]]*)\[/img]",@"<a href=""$1"" target=""_blank""><img src=""$1"" onload=""javascript:if(this.width>screen.width-220)this.width=screen.width-220"" border=1></a>",RegexOptions.IgnoreCase);
}
this.lbDescription.Text = " "+ToUrl(currentDR["Description"].ToString());//调用方法