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 Link to comment https://forums.phpfreaks.com/topic/123562-solved-a-little-preg_match-help-if-you-will/ 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. Link to comment https://forums.phpfreaks.com/topic/123562-solved-a-little-preg_match-help-if-you-will/#findComment-638167 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 Link to comment https://forums.phpfreaks.com/topic/123562-solved-a-little-preg_match-help-if-you-will/#findComment-638169 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 Link to comment https://forums.phpfreaks.com/topic/123562-solved-a-little-preg_match-help-if-you-will/#findComment-638174 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. Link to comment https://forums.phpfreaks.com/topic/123562-solved-a-little-preg_match-help-if-you-will/#findComment-640294 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; } Link to comment https://forums.phpfreaks.com/topic/123562-solved-a-little-preg_match-help-if-you-will/#findComment-640295 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.. Link to comment https://forums.phpfreaks.com/topic/123562-solved-a-little-preg_match-help-if-you-will/#findComment-640296 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.