Codefixer - ASP tutorials, resources and software
ASP.NET 3.5/2.0 Hosting – Click Here
Home   Articles   Resources   ASP directory   ASP Tutorials   Code Snippets   ASP Applications   Forum
Wednesday 14 May
MembersPro PayPal - ASP Membership software

asp.netPRO “Best ASP.NET Host” – DiscountASP.NET


ASP Partner ASP Partner ASP Partner more partners...

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
%>





If you have any code snippets to share with full credit given then send an email to Codesnippets - You'll receive full credit and a link back to your site.

ASP.NET 3.5/2.0 Web Hosting: 3 Months FREE – Click Here!




About | Contact | Advertise | Feedback | Hire Us | Link

Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.

Do you intend to move to ASP.NET or have you already?
Yes will do
Have done
ASP does fine
Not a priority


Directory Software