Censoring User Comments in a Guestbook/Forum Setting
By DerrickWhile 99% of your Web sites visitors are kind, positive people, there will always exist that 1% that aim to be an annoyance and will attempt to irritate as many people as possible. These folks always seem to find their way to your Guestbook or Messageboard and post inflammatory messages. Often times, they'll use abusive language that your polite visitors will find rude. Why let a few bad apples spoil the whole barrel?
Rather than confine yourself to periodically searching the Messageboards or Guestbooks on your site for
offensive language, why not have these posts automatically censored (if needed) when posted? The following
code provides a function, CensorStr
with the following definition:
Function CensorStr(NaughtyString)
|
CensorStr
takes one parameter, NaughtyString
, which is the string you want to
censor. The function returns a censored version of NaughtyString
. The code for
CensorStr
follows:
|
You can try out the above code. In the text box below, enter a string, and it will be censored. Note
that the only words that are censored are:
For each word that you want censored, add an entry in the
Limitations:cat
, chicken
, dog
,
horse
, and pig
; they will be censored with
c*t
, c*****n
, d*g
, h**se
, and #!#
,
respectively.
ExpletivesBAD
array, and a corresponding
"polite" word in the ExplietivesCEN
array. The CensorStr
function will do the rest for
you!
Note that CensorStr
checks for instances of the curse words that have a space either on the
right or left. This prevents unneeded words from being censored. For example, if "hell" is censored,
to be replaced with "heck", if a user posts:
Hello, world!
|
the result will still be:
Hello, world!
|
There are two drawbacks to this approach, though; first, those rotten apples can get around the censoring by simply posting a one word post, or not putting a space around their expletive. For example:
Go to *hell*.
|
would not be censored, since no spaces surround the word "hell"; second, since it searches for either a leading or trailing space, a sentence like:
Tell them I said hello.
|
Would be censored to:
Tell them I said hecko.
|
If you want to take out all instances of your censored words, you will need to make a slight change to the code. Simple take out the lines:
|
and replace it with:
|
Of course, be warned that all instances of the "naughty" words will be censored. For example, if you
have "hell" censored, our earlier Hello, world!
example would be censored to:
Hecko, world!
|
You can see the limitations present. Censoring illicit language is difficult, especially when attempting to
censor words that commonly appear in parts of words that appear in everyday language, like "hell". You will
find the censorship routine works much better at catching the very dirty words. In either case, you
are encouraged to tweak the CensorStr
function to obtain the results you desire.
This source code is free to use, of course! For more information on the Replace
function, be
sure to read the technical
documentation. You might also find the following technical docs helpful as well:
UBound
and
Array
.
Happy Programming!
Attachments:
CensorStr
in text format