woolyg Posted September 10, 2008 Share Posted September 10, 2008 Hi all, I'm trying to put together a 'bad word' filter on text-input form items that have been POSTed to a PHP file. Is there a way to put all curse words into an array, and check all of them against 1 string? All help appreciated! WoolyG Quote Link to comment Share on other sites More sharing options...
DamienRoche Posted September 10, 2008 Share Posted September 10, 2008 I'm not sure but maybe I can help enough so you can search google for the rest. What you need to do is be able to read a .txt file that has curse words in it. There is a function for that but not sure what it is. Thankfully, once that's done you can check it with a variable quite easily: $post = ////THE POST YOU ARE CHECKING $cursewords = ////READ .TXT FILE AND STORE IN THIS VARIABLE if (preg_match($cursewords,$post)){ echo "CURSE WORD DETECTED"; } else { echo "everybody is happy"; } Hope that gets you closer to your answer. Quote Link to comment Share on other sites More sharing options...
woolyg Posted September 10, 2008 Author Share Posted September 10, 2008 Thanks, I'm looking to do something like $mystring = "Hello I'm looking for a word like word1 or word2"; $cursewords = array('word1', 'word2'); $pos = strpos($mystring, $cursewords); echo $pos .. but attempting to plug an array into strpos() isn't working. Has anyone dealt with this kind of string stuff before? Thanks WoolyG Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted September 10, 2008 Share Posted September 10, 2008 You could loop through it. $string = "Hello I'm looking for a word like word1 or word2"; $cursewords = array("word1","word2"); $found = 0; foreach($cursewords as $word){ if(strpos($string, $word) !== false){ $found++; } } //if $found is greater then 0 then you have some bad words Quote Link to comment Share on other sites More sharing options...
woolyg Posted September 13, 2008 Author Share Posted September 13, 2008 ProjectFear - excellent, you've nailed it. Thank you very much - Solved. Quote Link to comment Share on other sites More sharing options...
xoligy Posted September 13, 2008 Share Posted September 13, 2008 i have something similar to the one projectfear posted this one blanks out the length of the word with * so if someone said the f word thats 4 stars if they said one with 8letters 8 stars etc etc. function filter_lang($Input) { $obscenities = array("*+*++*+*","*-*-*-*-*", ); foreach ($obscenities as $curse_word) { if (stristr(trim($Input),$curse_word)) { $length = strlen($curse_word); for ($i = 1; $i <= $length; $i++) { $stars .= "*"; } $Input = eregi_replace($curse_word,$stars,trim($Input)); $stars = ""; } } return $Input; } Quote Link to comment Share on other sites More sharing options...
woolyg Posted September 13, 2008 Author Share Posted September 13, 2008 Nice one! I'll use that for bulletin board programming, it'll come in handy.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.