profanity filter

class profanity extends CValidator


    public $banned_words = array('word1', 'word2', 'word3');

    protected function validateAttribute($model, $attribute)


        $errorMessage = "You must choose another username!";

        $value = $model->$attribute;

        foreach ($this->banned_words as $bw)

                if (preg_match("/".$bw."/i", $value))

                        $model->addError($attribute, $errorMessage);



you can use this in your model’s rules method like this:

array('username', 'profanity', 'on' => 'register'),

i know not the best approach but it works in the basic idea :)

for larger arrays it can be converted to db lookup.