请问用何种方式过滤非法字符,并保留回车空格。
都有什么做法呢?是否也有类似UBB一类的东西,可以在ASP.NET中使用呢?

解决方案 »

  1.   

    strContent=strContent.Replace("&","&amp");
    strContent=strContent.Replace("'","''");
    strContent=strContent.Replace("<","&lt");
    strContent=strContent.Replace(">","&gt");
    strContent=strContent.Replace("chr(60)","&lt");
    strContent=strContent.Replace("chr(37)","&gt");
    strContent=strContent.Replace("\"","&quot");
    strContent=strContent.Replace(";",";");
    strContent=strContent.Replace("\n","<br/>");
    strContent=strContent.Replace(" ","&nbsp");
    return strContent;
      

  2.   

    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;");
    retVal=retVal.Replace("\r", "<br>");
    return retVal;
    } public static string OutputText(string inputString)
    {
    string retVal=inputString;
    retVal= ConvertStr(retVal);
    retVal=retVal.Replace("
    retVal=retVal.Replace("">", "");
    retVal=retVal.Replace("
    ", "");
    retVal=retVal.Replace("", "");
    retVal= Regex.Replace(retVal,@"\[flash=\d+,\d+](?<x>[^\]]*)\[/flash]",@"$1",RegexOptions.IgnoreCase);
    retVal=retVal.Replace("[flash]", "");
    retVal=retVal.Replace("[/flash]", "");
    return retVal;
    } public static string ToUrl(string inputString)
    {
    string retVal=inputString;
    retVal= ConvertStr(retVal);
    retVal= Regex.Replace(retVal,@"\
    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,@"\",@"<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);
    }
      

  3.   

    我有详细的一个用于处理的类,包括UBB处理,如果你需要请留下你的Email,我发给你。
      

  4.   

    <%@ import Namespace="System.Web" %>
    <td>
    <%# HttpUtility.HtmlEncode(DataBinder.Eval(Container.DataItem, "ID").ToString())%>
    </td>
      

  5.   

    过滤(解码):
    StringWriter writer = new StringWriter();
    Server.HtmlDecode(str,writer);
    string tmp = writer.ToString();还原(编码):
    StringWriter writer = new StringWriter();
    Server.HtmlEncode(str,writer);
    string tmp = writer.ToString();目前在我开发项目中就是使用方法过滤html代码的。效果还行,同时空格可以保留。
      

  6.   

    to: v192(魔渡众生)[email protected]多谢多谢!
      

  7.   

    晕,Server.HtmlEncode(...) 足够了~~
      

  8.   

    http://blog.csdn.net/johnsuna/archive/2004/12/05/FilterRealProxy.aspx