1 Replies - 1149 Views - Last Post: 26 March 2013 - 02:42 PM Rate Topic: -----

#1 matn89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-February 13

PHP word filter problem...

Posted 26 March 2013 - 01:57 PM

Hi guys

I have the following code :


	public static function filter($message)
	       $messagearray = explode(" ", $message);
        foreach($messagearray as $word)
       	$bannedword = self::getbannedwords($word);
       	 	$word_found  = $banned['word'];
       	 	$new_word = 'PROFANITY';
       	 	$key = array_search($word_found, $messagearray);

      	 	$replace = array($key => $new_word);
       	 	$messagearray = array_replace($messagearray, $replace);

        $newmessage = implode(" ", $messagearray);


       return $newmessage;


This function is intended to find any words which are found bannedwords table of my db ( i am using pdo) It is retrieving the words fine. So say if a message contained the words s**t and f**k they would both be put into the $bannedword array.

Each bad word is supposed to be replaced with ( for now) the word "profanity" but it only appears to be changing the first bad word it encounters.

So if the input is f**K s**t then the output is profanity s**t.

Can anyone see in my code why this might be happening ?

Thanks for your time!

Is This A Good Question/Topic? 0
  • +

Replies To: PHP word filter problem...

#2 creativecoding   User is offline

  • Hash != Encryption
  • member icon

Reputation: 931
  • View blog
  • Posts: 3,216
  • Joined: 19-January 10

Re: PHP word filter problem...

Posted 26 March 2013 - 02:42 PM

This is an odd way on going about this problem. Is it necessary? People can just insert character between their words like "b ad" or "b_ad" and you won't be able to match it.

A solution I would go with is stripping all non A-Z characters (keeping or removing spaces, depends on how strict you wanna be) and then going through it looking for bad words. You can use strpos and strlen to detect the bad words (first by finding the position of where the bad word starts, x, and then the length of the bad word, y, the whole bad word will be x to x+y in your string). To put the string back together how it used to be you'll have to create a way to store where the non A-Z character were before the badword detection, and reinsert them after you're done. But that's another problem :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1