bad word filtering asp.net

a8le

Well-known member
Joined
Oct 27, 2005
Messages
75
Programming Experience
5-10
Hi all,

I am trying to upgrade my bad words filter, which is this:

VB.NET:
                Page_Load...
                Dim xmlDocPath As String = MapPath("BadWordsList.xml")
                Dim xmlReader As XmlTextReader = New XmlTextReader(xmlDocPath)
                While (xmlReader.Read())
                    If xmlReader.NodeType = XmlNodeType.Text Then
                        alWordList.Add(xmlReader.Value)
                        Trace.Write("Added: " & xmlReader.Value)
                    End If
                End While
                xmlReader.Close()

        Public Function CheckString(ByVal InputString As String) As String
            Dim r As Regex
            Dim element As String
            Dim output As String
            Trace.Write("Checking " & InputString)
            For Each element In alWordList
                r = New Regex("\b" & element)
                Trace.Write("Checking: " & element)
                InputString = r.Replace(InputString, "")
            Next
            Trace.Write("Returning " & InputString)
            Return InputString
        End Function

I am trying to follow this tutorial (found from google'ing: bad word filter asp.net)...

http://www.codefixer.com/codesnippets/replacebadwords.asp

Coincidentally, my code is outputting their example 1. I want to get to example 2. I have tried and tried to merge the code, but can't get it. Please help.

Thanks in advance,
a8le
 
So instead of replacing the bad word with empty string "" you want to replace it with the first letter of the bad word and * chars for the rest of the word?

"element" is that word, translate it to the target string and use it to replace with.

For example element variable is "badie":
VB.NET:
Dim replace As String = element(0) & New String("*", element.Length - 1)
replace variable is now: b****
 
Hi JohnH,

Thank you for the help and words of wisdom. I got it working! Here's the code for others:

VB.NET:
Public Function CheckString(ByVal InputString As String) As String
            Dim r As Regex
            Dim element As String
            Dim output As String
            Dim eLength As Integer
            Dim x As Integer
            Dim AttachtoEnd as String
            Trace.Write("Checking " & InputString)
            For Each element In alWordList
                r = New Regex("\b" & element)
                Trace.Write("Checking: " & element)
                eLength = element.Length
                For x = 1 To eLength - 1
                    AttachtoEnd = AttachtoEnd & "*"
                Next
                InputString = r.Replace(InputString, element, Left(element, 1) & AttachtoEnd)
                AttachtoEnd = ""
            Next
            Trace.Write("Returning " & InputString)
            Return InputString
        End Function

I hope someone finds this piece of code useful.

-a8le
 
Make it an "element", add it to the list like other "words".
 
Back
Top