.net中让LABEL接受的文本自动分行并且过滤多余的字符或者恶意字符

文本过滤与格式化方法
本文介绍了一种用于文本过滤和格式化的实用方法,通过替换特定字符来防止恶意输入,并确保文本在网页上正确显示。此外,还展示了如何将文本转换为URL友好的格式。
lbnr.Text=ToUrl(dr["Remark"].ToString());//具体到某个控件的text显示的内容引用


/// <summary>
  /// 让LABEL接受的文本自动分行并且过滤多余的字符或者恶意字符
  /// </summary>
  /// <param name="inputString"></param>
  /// <returns></returns>
  public static string ConvertStr(string inputString)//定义
  {
   string retVal=inputString;
   retVal=retVal.Replace("&","&amp;"); //过滤字符&
//   retVal=retVal.Replace("\"","&quot;"); //过滤字符\
//   retVal=retVal.Replace("<","&lt;"); //过滤字符<
//   retVal=retVal.Replace(">","&gt;"); //过滤字符>
//   retVal=retVal.Replace(" ","&nbsp;"); //过滤字符空格" "
//   retVal=retVal.Replace("  ","&nbsp;&nbsp;"); //过滤字符" "" "
//   retVal=retVal.Replace("\t","&nbsp;&nbsp;");//过滤字符\t
   retVal=retVal.Replace("\r", "<br>");//过滤字符\r<br>
   return retVal;
  }
  public static string ToUrl(string inputString)//引用并替换
  {
   string retVal=inputString;
   retVal= ConvertStr(retVal);
   return 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);
  }
  

转载于:https://www.cnblogs.com/Luoke365/archive/2007/06/15/784131.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值