Jump to content

php bad word checker


chmpdog

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.