Replacing Bad words in a String

Below are two examples of how to deal with bad words. Example 1 receives text entered into a form textfield called 'txtComments'. The code strips out any bad word(s) that you choose to enter into the array 'badChars'. In this script we have deemed that the words 'rubbish', 'crap' and 'shit' are unacceptable and should be removed.

Example 1:

<%
Dim sComments
sComments = ReplaceBadWords(Request.Form("txtComments"))
response.write sComments

Function ReplaceBadWords(InputComments)
Dim badChars, newChars, i
'create our array of bad words
badChars = array("rubbish", "crap", "shit")
newChars = InputComments
for i = 0 to uBound(badChars)
newChars = Replace(newChars, badChars(i), "")
Next
ReplaceBadWords = newChars
End function
%>

Rather than simply just removing the bad word(s) below is a script that will take a bad word such as shit and replace it with s***. The script gets the first letter of the bad word and replaces all the other letters with *.

Example 2:

<%
Dim sMyString
sMyString = ReplaceBadWords("this is a rubbish crap bad word filter")
response.write sMyString

Function ReplaceBadWords(InputComments)
Dim badChars, newChars, sLength, sAttachtoEnd, x, i
'create an array of bad words that should be filtered
badChars = array("rubbish", "crap", "shit")
newChars = InputComments
'loop through our array of bad words
For i = 0 to uBound(badChars)
'get the length of the bad word
sLength=Len(badChars(i))
'we are going to keep the first letter of the bad word and replace all the other
'letters with *, so we need to find out how many * to use
For x=1 to sLength-1
sAttachtoEnd=sAttachtoEnd & "*"
Next
'replace any occurences of the bad word with the first letter of it and the
'rest of the letters replace with *
newChars = Replace(newChars, badChars(i), Left(badChars(i),1) & sAttachtoEnd)
sAttachtoEnd=""
Next
ReplaceBadWords = newChars
End function
%>

ASP/.NET Hosting

ASP.NET Web Hosting – 3 Months Free!

MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.