To read the article online, visit

Censoring User Comments in a Guestbook/Forum Setting

By Derrick

While 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:

Function CensorStr(NaughtyString)

  'this array defines the words to look for and what to
  'replace the word with if found.
  ExpletivesBAD = Array("cat","chicken","dog","horse","pig")
  ExpletivesCEN = Array("c*t","c*****n","d*g","h**se","#!#")

  TotalExpletives = ubound(expletivesBAD)

  'This for loop loops through all of the expletives,
  'replacing them with the appropriate string if they exist
  'in NaughtyString
  for i = 0 to TotalExpletives
 	CENSOR = InStr(1, NaughtyString, ExpletivesBAD(i), 1)
	if CInt(CENSOR) > 0 then
		'Note that two versions of the expletive are searched.  We
		'use these two variations to we can be sure that we do
		'not catch a nonexpletive accidentally.  For example, if
		'we want to censor "hell," we want to make sure we do not
		'censor "hello"
		NaughtyString = replace(NaughtyString," " & _
		NaughtyString = replace(NaughtyString,ExpletivesBAD(i) & _
		        " ",ExpletivesCEN(i),1,-1,1)
	end if

  CensorStr = NaughtyString
End Function

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: cat, chicken, dog, horse, and pig; they will be censored with c*t, c*****n, d*g, h**se, and #!#, respectively.

Enter string to be censored:

For each word that you want censored, add an entry in the 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:

	NaughtyString = replace(NaughtyString," " & _
	NaughtyString = replace(NaughtyString,ExpletivesBAD(i) & _
	        " ",ExpletivesCEN(i),1,-1,1)

and replace it with:

	NaughtyString = replace(NaughtyString, _

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!


  • Source code for CensorStr in text format

  • Article Information
    Article Title: Censoring User Comments in a Guestbook/Forum Setting
    Article Author: Derrick
    Published Date: Wednesday, February 09, 2000
    Article URL:

    Copyright 2019 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers