Feb 222012

I needed a profanity filter for the highscores in an app I made, but I couldn’t find anything free that worked the way I wanted, so I made a simple one myself. It’s pretty basic but someone might have use for it.

You need a list of lowercase words to filter for. The one I used is far too big to paste here, but you can find one by searching for “bad word list”.

private static const badWords:Array = ["lots","of","bad","words"];
// Returns a profane word if found, else ""
static public function checkName(text:String):String {
  text = text.toLowerCase();
  for (var i:int = 0; i < charReplacements.length; i++ ) {
    var ra:Array = charReplacements[i] as Array;
    text = strReplace(text, ra[0], ra[1]);
  for each (var w:Object in badWords) {
    if (text.indexOf(String(w)) != -1) {
      return String(w);
  return "";

The function will detect character sequences that look like some other character, to make it harder to bypass the filter. For example, 1 becomes i, û becomes u, |\| becomes n etc.
In this way “Fuc|<” would be detected for instance. The sequence replacements can even be used to identify inappropriate symbols like “(.)”, by converting them to (in)appropriate words if you like. 🙂

private static const charReplacements:Array = [["@", "a"], ["0", "o"], ["1", "i"],
  ["2", "r"], ["3", "e"], ["4", "a"], ["5", "s"], ["7", "t"], ["8", "b"],
  ["9", "g"], ["|<", "k"], ["|\/|", "m"], ["|\|", "n"], ["ä", "a"], ["ã", "a"],
  ["â", "a"], ["ä", "a"], ["á", "a"], ["à", "a"], ["å", "a"], ["é", "e"],
  ["è", "e"], ["ë", "e"], ["ê", "e"], ["§", "s"], ["$", "s"], ["£", "l"],
  ["€", "e"], ["ü", "u"], ["û", "u"], ["ú", "u"], ["ù", "u"], ["î", "i"],
  ["ï", "i"], ["í", "i"], ["ì", "i"], ["ÿ", "y"], ["ý", "y"], ["ö", "o"],
  ["ô", "o"], ["õ", "o"], ["ó", "o"], ["ò", "o"], ["(.)","boob"]];

You will also need this helper function for string replacement:

// Helper that replaces all "find" with "replace" in the given input string
public static function strReplace(input:String, find:String, replace:String):String {
  while (input.indexOf("find") != -1)
    input = input.split(find).join(replace);
  return input.split(find).join(replace);

It works quite well, let me know if you found it useful or have any suggesstions.