Re: adult content censoring

From: Date: Sun, 25 Apr 2004 15:28:37 +0000
Subject: Re: adult content censoring
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-184566@lists.php.net to get a copy of this message
* Thus wrote Richard Davey (rich@launchcode.co.uk): > Hello Andy, > > Sunday, April 25, 2004, 3:22:29 PM, you wrote: > > AB> i have to make an adult content censoring system so people cant post "bad" > AB> words on any public viewable posts. i know preg_replace and possibaly > AB> preg_match would be a huge help but im wondering how i would put all the bad > AB> words in a file (textfile) 1 word on a line and have it look through the > AB> file for the words? when it finds a match then it will either replace the > AB> word with * but better yet tell the user that content in the post was > AB> rejected because of bad content and take them back to the form with the > AB> stuff they typed in it... > > The way I do it on a piece of forum software I wrote on a VERY popular > web site was as follows: > > Create an include file which contains all your badwords in an array, > like: > > $badword[] = 'well'; > $badword[] = 'you'; > $badword[] = 'get'; > $badword[] = 'the'; > $badword[] = 'idea'; > > Then I have a simple function as follows: > > function APL_Func_BadWord ($text) > { > global $badwords; > > // This will check our given text against the badword list. It does it > slightly differently > // in that instead of firing up the regular expression engine, we use a > more simple but just as effective technique: One advantage with using preg*, is you can have more flexible matching instead of having a real large list of bad words: $badword[] = '/suck(er|ing|ed)?/i'; Also instead of str_replace scanning the whole string 5 time to replace the 5 items, its only scanned once, replacing up to five things. So depending on how large your list of words are and how large the content is you scanning, preg_* might be a little more efficient. And then there is the possiblity that the users might get smart and start entering things like: s-u-c-k One way to fix that is to replace all Non-word characters in the subject string with null: preg_replace('/\W/', '') Of course that wont work if they use L337 or html tags. Curt -- "I used to think I was indecisive, but now I'm not so sure."

« previous php.general (#184566) next »