chmpdog Posted June 4, 2010 Share Posted June 4, 2010 why won't this work... it only outputs 0 when it should output 1 error_reporting (E_ALL); function checkname($str) { $badWordsFile="badwords.txt"; $badFlag = 0; if(!is_file($badWordsFile)) { die("ERROR: file missing: ".$badWordsFile); } else { $badWordsFH = fopen($badWordsFile,"r"); $badWordsArray = explode("\n", fread($badWordsFH, filesize($badWordsFile))); fclose($badWordsFH); } $i =0; while($i < count($badWordsArray)) { if(stristr($str, $badWordsArray[$i]) !== FALSE) { $badFlag = 1; } $i++; } return $badFlag; } echo checkname("sluts"); Link to comment https://forums.phpfreaks.com/topic/203818-php-bad-word-checker/ Share on other sites More sharing options...
BizLab Posted June 4, 2010 Share Posted June 4, 2010 i would begin by pulling the "bad words" out of the text file and into the array to be used in the comparison. I would also use the strpos method to search the array since it will pull thewordshitout of that string, and the word does not need to be isolated. When using strpos($content to check, $array of bad words) i found this on the php.net site - - a good example <?php // strpos that takes an array of values to match against a string // note the stupid argument order (to match strpos) function strpos_arr($haystack, $needle) { if(!is_array($needle)) $needle = array($needle); foreach($needle as $what) { if(($pos = strpos($haystack, $what))!==false) return $pos; } return false; } ?> you can return $pos to find the matched words, OR just return true and use that. Link to comment https://forums.phpfreaks.com/topic/203818-php-bad-word-checker/#findComment-1067481 Share on other sites More sharing options...
chmpdog Posted June 4, 2010 Author Share Posted June 4, 2010 I pulled it out of the text file... I can echo this: $badWordsArray[1] but it won't work even if I copy and paste the text and make it as simplistic as possible function arr($haystack, $needle) { foreach($needle as $what) { if(($pos = strpos($haystack, $what))!==false) return $pos; } return "fardar"; } echo $badWordsArray[1]; echo arr("thewordinspace1", $badWordsArray[1]) Link to comment https://forums.phpfreaks.com/topic/203818-php-bad-word-checker/#findComment-1067489 Share on other sites More sharing options...
ignace Posted June 4, 2010 Share Posted June 4, 2010 Your bad word filter is going to be a little more complex. You would want to catch for example sh*t (which is not caught by your regular filter which searched for shit), you can do that with preg_match('/sh?t/', $badWord) you also would want to replace all regularly used symbols for their letter counterparts eg $h*t which you would replace with sh*t and then replace ? with any symbol that does not have a proper letter counterpart. You would also want to remove symbols that are used to fool these filters eg s.h.i.t. etc.. Link to comment https://forums.phpfreaks.com/topic/203818-php-bad-word-checker/#findComment-1067573 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.