Forum Moderators: open

Message Too Old, No Replies

Problems with the REPLACE() function in ASP

Need to match exact word

         

webboy1

10:41 am on Mar 21, 2004 (gmt 0)

10+ Year Member



Hi,

Im not sure if anyone can actually help me with this. I am trying to filter out swear words if they are entered into a forum i am building.

I do actually have the script working. So if someone enters say "you are a (sear word)", the code replaces the swear word with the relevany number of "*"'s.

So in this sense i am quite happy with it. The swear words are all stored in a DB, and basically when someone starts a new thread or replies to an existing thread, there comments are checked for any words matching those in the DB. If there is a match, the word is replaced.

Sounds fine.

However, the problem comes in when someone types a word that contains a swear word, but itself is not actually a swear word.

For example, if someone entered "I support arsenal FC", the script would return "I support ****nal" because it has replaced the instance of arse.

Does anyone know of a way i can get around this? I have tried entering the words into the database as (space)swearword(space), but if the swear word is the first or last word in the message then it is not picked up.

Is there anyway i can count the characters of the word in the string to make sure it matches the length if the swear word i.e. arse is 4 characters long, arsenal is 7, so do not replace the arse part?

The code I am using is just the simple Replace function:

Message = Replace(usermessage, wordfromDB, numberofstars)

All help appreciated.

Cheers
Webboy

WebJoe

11:14 am on Mar 21, 2004 (gmt 0)

10+ Year Member



different approach: add space-characters to the beginning or end of the swear words...there is no " arse " in arsenal. AFAIK you can't check for the length of the compared string

webboy1

11:32 am on Mar 21, 2004 (gmt 0)

10+ Year Member



I have tried this method, and although it does stop the "arsenal" scenarios, it throws up another problem.

if the message entered by the user was "you are an arse", the word arse would not be picked up, because it does not match the " arse " in the database. There is no space after it.

Looks like this is going to be a tricky one to solve!

Any more thoughts would be appreciated.

bread_man

5:18 pm on Mar 24, 2004 (gmt 0)



I'm trying to remove certain words from search strings for my search application and was plauged with the exact same problem. I found this article which teaches how to use regular expressions. They're what you need in order to replace exact phrases with a word boundaries. Check out all 3 pages of the article - it was exactly what I needed.

[4guysfromrolla.com...]

HyperGeek

7:24 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



You can match the length of the stop word as well.

For instance, you can say:

IF word = RS("word") AND Len(word) = Len(RS("word")) THEN
...
CALL wordFilter()
'which would Len("word") and place a "*" times the character length of the filtered word.
...
END IF