在论坛上在发表的时候评论的时候,输入的内容去掉用户输入的诸如‘我操|我日|操你妈|靠|傻逼’等等这些文本,如何更好的用asp.net制作一个程序过滤掉这些文字?谢谢,有整个例子是最好的,谢谢各位大虾的帮助!

解决方案 »

  1.   

    Regex.Replace("我操|我日|操你妈|靠|傻逼文本","我操|我日|操你妈|靠|傻逼","*");
      

  2.   

    LZ骨骼一下 fanhexie.tk 偶自己做了个 遍历词库
    Imports System.IO
    Partial Class wordtool_dohx
        Inherits System.Web.UI.Page
        Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
            Dim strRtnWord As String
            Dim strWord As String = Request.Form("str")
            Dim strFilter As String = Request.Form("strFilter")        'Dim strWord As String = "今天我们去游行哈哈哈 反日吧。。个哦个哦个哦过"
            'Dim strFilter As String = "/*/"
            If Not IsPostBack Then            strRtnWord = doFilterword(strWord, strFilter)            Response.Write(strRtnWord)
                Response.End()
            End If    End Sub    Public Function doFilterword(ByVal i_strWord As String, ByVal i_strFilter As String) As String
            Dim strTemp As String = ""
            Dim strArr As String = ""
            Dim strFilter As String = ""
            strTemp = i_strWord
            For i As Integer = 0 To CType(Application("arrList"), ArrayList).Count - 1
                strArr = CType(Application("arrList"), ArrayList)(i).ToString
                strFilter = ""
                For j As Integer = 0 To strArr.Length - 1
                    strFilter = strFilter & strArr(j) & i_strFilter
                Next            strTemp = strTemp.Replace(strArr, strFilter)
            Next
            Return strTemp
        End Function
    End Class<%@ Application Language="VB" %>
    <%@ Import Namespace="System.IO" %>
    <script runat="server">
        
        Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
            ' Code that runs on application startup
            loadWordData()
        End Sub
        
        Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
            ' Code that runs on application shutdown
        End Sub
            
        Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
            ' Code that runs when an unhandled error occurs
        End Sub    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
            ' Code that runs when a new session is started
        End Sub    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
            ' Code that runs when a session ends. 
            ' Note: The Session_End event is raised only when the sessionstate mode
            ' is set to InProc in the Web.config file. If session mode is set to StateServer 
            ' or SQLServer, the event is not raised.
        End Sub
        Public Sub loadWordData()
            Dim arrList As ArrayList
            arrList = New ArrayList        Dim sr As StreamReader = New StreamReader(Server.MapPath("~/") + "\word.txt")        Do While sr.Peek() >= 0
                arrList.Add(Trim(sr.ReadLine()))
            Loop
            sr.Close()
            
            Application("arrList") = arrList
                End Sub
    </script>
      

  3.   

    在后台设置过滤字符
    void Application_BeginRequest(object sender, EventArgs e)
    {
      for (int i=0; i < Request.Form.Count;i++)
      {
      if (Request.Form[i].ToString() == "__VIEWSTATE") continue;
      if (IsM(Request.Form[i].ToString()))
      {
      Response.Write("您提交的内容中含有非法字符.");
      Response.End();
      }  }   
    }
    protected bool IsM(string InText)
    {
      string word = @"";
      if (InText == null)
      return false;
      if (Regex.IsMatch(InText,word))
      return true;
      return false;
    }   
      

  4.   


     看来csdn没这么先进的过滤系统。
      

  5.   

    这个应该再改进一下,因为这种情况是没法过滤“我我操操”这种情况的(这种情况过滤后还是“我操”),所以用个循环while(Regex.Match(我操|我日|操你妈|靠|傻逼文本","我操|我日|操你妈|靠|傻逼").Success)
    {
        Regex.Replace("我操|我日|操你妈|靠|傻逼文本","我操|我日|操你妈|靠|傻逼","*");
    }
      

  6.   


    while(Regex.Match(我操|我日|操你妈|靠|傻逼文本","我操|我日|操你妈|靠|傻逼").Success)
    {
        Regex.Replace("我操|我日|操你妈|靠|傻逼文本","我操|我日|操你妈|靠|傻逼","*");
    }
    先判断是不是匹配,然后替换
      

  7.   

    用C#的话,.net framework 5.0将自带脏话过滤函数:System.Text.ZhangHua.Filter();
      

  8.   

    自己新建一个脏话的表,然后时刻的进行脏话的更新,如果数据不多,就缓存在内存中,如果数据 量很大,你可以缓存热门的脏话用语,然后进行replace,就可以了啊。