?
Solved

Bad Words Filter

Posted on 2013-11-07
3
Medium Priority
?
385 Views
Last Modified: 2013-11-08
Hi Guys

The following function takes a string of characters (words) and filters through them looking for "bad words" - the bad words are contained in an array - what I would like to do is use a text file which has one bad word per line - so I need to load the text file, line by line into an array - how would I do that? I know I can do that by using a table, but if I use a textfile, several programs can use it with less overhead.

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", "cunt", "bastard", "prick", "fuck")
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

Open in new window


MTIA

DWE
0
Comment
Question by:dwe0608
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
Big Monty earned 2000 total points
ID: 39633526
here's a function you can use to dump a text file into an array:

Function File(filename)	
    Dim fso,ts, myarray

    Const ForReading = 1
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.OpenTextFile(filename, ForReading)

    reDim myarray(0)	

    i=-1

    While not ts.AtEndOfStream
      i=i+1
	
      Redim Preserve myarray(i)
      myarray(i)=ts.ReadLine
    Wend

    ts.Close
    File=myarray
  End Function

Open in new window


source
0
 
LVL 1

Author Closing Comment

by:dwe0608
ID: 39634806
Thanks greatly for the function
0
 
LVL 1

Author Comment

by:dwe0608
ID: 39634812
This is how I implemented the function ... and I've attached the bad words file I have used as well.

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", "cunt", "bastard", "prick", "fuck")
dim fn
fn = server.mappath("badwords.txt")
badchars = BadWordsFile(fn)
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
' note there is no error checking
' if we pass a file with nothing in it, it may throw an error
Function BadWordsFile(filename)	
    Dim fso,ts, myarray
    Const ForReading = 1
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.OpenTextFile(filename, ForReading)
    reDim myarray(0)	
    i=-1
    While not ts.AtEndOfStream
      i=i+1
      Redim Preserve myarray(i)
      myarray(i)=ts.ReadLine
    Wend
    ts.Close
    set ts = nothing
    set fso = nothing
    BadWordsFile=myarray
End Function

Open in new window

badwords.txt
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question