[ask] badword filter script

ogah

New member
i want to make bad words filter scrip with php

example case:
i want to filter the word "out"
if someone submitting the words containing "out" with or without leading or trailing space, it will be rejected.
but word like "outfit" or "without" will be accepted.

can anybody help me?

thanks
 

ogah

New member
jaran said:
The easy way is using similar word API. You can find alot at google. I found an example from Wordnik API which they have related words features on their API.

http://developer.wordnik.com/docs.html#!/word/getRelatedWords_get_4

Code:
http://api.wordnik.com:80/v4/word.json/f*ck?useCanonical=false&includeSuggestions=false&api_key=a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5
response body
Code:
{
  "id": 0,
  "scrabble": false,
  "lookupCount": 0
}
how to know the "f*ck" is bad word?
 

jaran

New member
For specific words like this, I usually using regex to replace all of them. You may try it now.
PHP:
<?php
$old_string = 'You are bicth fool. Fuck yourself !!!';
$bad_words_list = '/crap|ugly|brat|fool|fuck|fucking|f\*cking|f\*ck|bitch|b\*tch|shit|sh\*t|fool|dumb|couch potato|arse|arsehole|asshole|\*ssh\*l\*|\*\*\*\*|c\*ck|\*\*\*\*sucker|c\*cks\*ck\*r|\*\*\*\*|c\*nt|dickhead|d\*c\*h\*a\*|\*\*\*\*|f\*c\*|\*\*\*\*wit|f\*ckw\*t|fuk|f\*k|fuking|f\*k\*ng|mother\*\*\*\*er|m\*th\*rf\*ck\*r|\*\*\*\*\*\*|n\*gg\*r|pussy|p\*ssy|\*\*\*\*|sh\*t|wanker|w\*nk\*r|wankers|w\*nk\*rs|whore|wh\*r\*|slag| sl\*g|\*\*\*\*\*|b\*tch|f u c k|f\*c\*|b.i.t.c.h|b\*tch|d-i-c-k|d\*\*\*/si';
$change_word = 'INSERT WORDS YOUR DESIRE';
$fix_words = preg_replace($bad_words_list, $change_word, $old_string);
?>
 

ogah

New member
thanks bro.
i want to reject submition if containing badword, not replace the badword with other word.
like example above. "out", " out" and "out " are rejected. but "out*", "*out" and "*out*" are accepted
 

ogah

New member
PHP:
$badword = array('word1', 'word2', 'word3','etc');
$ban = "/\b(".implode('|', $badword).")\b/siu";
if(preg_match($ban, $post)) {
	echo 'Sorry bad word not allowed';
}
else { echo 'ok'; }
 

simon

New member
if that you runs a user genarated content site you'd check that with your php script. maybe a fast way for your request