Page 1 of 1

Bad Word Censor [UNSOLVED-AGAIN]

Posted: Mon May 29, 2006 9:28 pm
by tecktalkcm0391
I have an array of bad words:

Code: Select all

<?php

$badwords = array();

//   Works as 
//   $badwords[] = array(' badword ', ' goodword ');

    $badwords[] = array(' \@\$\$ ', ' * ');
    $badwords[] = array(' a\$\$ ', ' * ');
    $badwords[] = array(' anton ', ' * ');
    $badwords[] = array(' arse ', ' * ');
    $badwords[] = array(' arsehole ', ' * ');
    $badwords[] = array(' ass ', ' * ');
	$badwords[] = array(' a s s ', ' * ');
	$badwords[] = array(' a.s.s ', ' * ');
	$badwords[] = array(' a.s.s. ', ' * ');


?>
Is there a way to have PHP add symbols between the letters in each array where the badword is? I am trying to do this so that I don't have to keep entering the same word and adding marks between them. This badword filter is for a website for kids, so I need everything to be blocked. And you know how tricky some kids can get.

Posted: Mon May 29, 2006 10:17 pm
by Ambush Commander
Iterate the array and insert the spaces.

Although I would probably move the bad word list to a file/database.

Posted: Mon May 29, 2006 10:20 pm
by tecktalkcm0391
what do you mean ?

Posted: Mon May 29, 2006 10:23 pm
by Ambush Commander
Actually, I was mistaken. You probably want to use regular expressions for this. Like...

Code: Select all

$clean = preg_replace('/(a\bs\bs|other|bad|words|use|\b|to|indicate|a|word|boundary)/', '*', $possibly_dirty);

Posted: Mon May 29, 2006 10:55 pm
by neogeek
This is the script that I used to censored words:

Code: Select all

$word_filter = explode("\n", @file_get_contents('word_filter.txt'));
while (list($key, $value) = @each($word_filter)) { $conversation = ereg_replace('[[:<:]](' . $value . ')[[:>:]]', '<span class="censor">censored!</span>', $conversation); }

Posted: Tue May 30, 2006 12:21 am
by hawleyjr

Array problem

Posted: Tue Jun 06, 2006 8:12 pm
by tecktalkcm0391
Can someone tell me how to fix this so that it returns the badword not 'array'. I would also like to change it if possible so it does the check like this:

Code: Select all

if(str_replace($username_bad_check, $badword[***A number that matches the array lines, that keeps going up for every bad word***] ...

Code: Select all

$username_bad_check = '12bad74words';
$username_bad_check = strtolower($username_bad_check);
$badwords = array();
	$badwords[] = array('bad', ' * ');
	$badwords[] = array('words', ' * ');
array_change_key_case($badwords,CASE_LOWER);
/*$username_bad_check = implode("\r\n", $username_bad_check); */
foreach ($badwords as $bword) 
{ 
	  if (str_replace($username_bad_check, $badword, $bword)){
	echo "OH NO YOU DID'T. We have found a bad word in your username! You may have put something bad in by mistake, but remember if you use bad words* on our site.<br><b>Your username can not contain the word </b>" . $bword . "<br><br><br><Br>*The term \"Bad Word\" refeers to a curse words, an explicit words, or any other inappropriate word, or word we find to be inappropriate." ; 
	exit;
    } 
}


So if anyone could help me fix this it would be appreciated. Also I got an error that it couldn't convert array to string, it only came up once though. anybody know why it would have come up?

Thanks!

Re: Array problem

Posted: Tue Jun 06, 2006 8:41 pm
by neophyte
See my notes in the code.
tecktalkcm0391 wrote:

Code: Select all

$username_bad_check = '12bad74words';
$username_bad_check = strtolower($username_bad_check);
$badwords = array();
 ///HERE IS YOUR PROBLEM
	$badwords[] = array('bad', ' * ');
	$badwords[] = array('words', ' * ');
array_change_key_case($badwords,CASE_LOWER);
/*$username_bad_check = implode("\r\n", $username_bad_check); */
foreach ($badwords as $bword) 
{ 
     
	  if (str_replace($username_bad_check, $badword, $bword)){
//This should echo out your "badword"         
echo $bword[0];
	echo "OH NO YOU DID'T. We have found a bad word in your username! You may have put something bad in by mistake, but remember if you use bad words* on our site.<br><b>Your username can not contain the word </b>" . $bword . "<br><br><br><Br>*The term "Bad Word" refeers to a curse words, an explicit words, or any other inappropriate word, or word we find to be inappropriate." ; 
	exit;
    } 
}

Posted: Tue Jun 06, 2006 9:31 pm
by tecktalkcm0391
THANKS!

Posted: Tue Jun 06, 2006 10:44 pm
by tecktalkcm0391
I am using this code:

Code: Select all

$username = 'bad12434word';

$username_bad_check = $username;
$username_bad_check = strtolower($username_bad_check);
$badwords = array();
	$badwords[] = array('bad', ' * ');
	$badwords[] = array('word', ' * ');
array_change_key_case($badwords,CASE_LOWER);
/*$username_bad_check = implode("\r\n", $username_bad_check); */
foreach ($badwords as $bword) 
{ 
	  if (str_replace($username_bad_check, $badwords, $bword)){
	echo "OH NO YOU DID'T. We have found a bad word in your username! You may have put something bad in by mistake, but remember if you use bad words* on our site.<br><b>Your username can not contain the word: </b>" .  $bword[0] . "<br><br><br><Br>*The term \"Bad Word\" refeers to a curse words, an explicit words, or any other inappropriate word, or word we find to be inappropriate." ; 
	exit;
    } 
}
And I am getting an error that say:
Notice: Array to string conversion in /home/cmmvideo/public_html/myvirtuallife.com/Login/user.php on line 123
Can anyone help me fix this?

Posted: Wed Jun 07, 2006 3:09 am
by twigletmac
You aren't using str_replace() correctly, it should be str_replace('word(s) to replace', 'word(s) to replace them with', 'string with word(s) that need(s) replacing). Although, I'm not even sure why you are using str_replace() here at all as you never actually test for the bad words, it will give them the error message no matter what. Try the following instead:

Code: Select all

$username = 'bdad12434word';

$username_bad_check = $username;
$username_bad_check = strtolower($username_bad_check);

// you only need an array of the words you are checking for
$badwords = array('bad', 'word');

foreach ($badwords as $bword) {
	if (strstr($username_bad_check, $bword)) {
		echo "OH NO YOU DON'T. We have found a bad word in your username! You may have put something bad in by mistake, but remember if you use bad words* on our site.<br><b>Your username can not contain the word: </b>" .  $bword . "<br><br><br><Br>*The term \"Bad Word\" refers to a curse words, an explicit words, or any other inappropriate word, or word we find to be inappropriate.";
		break;
	}
}
Mac

Posted: Fri Jun 09, 2006 9:10 pm
by tecktalkcm0391
Although, I'm not even sure why you are using str_replace() here at all as you never actually test for the bad words, it will give them the error message no matter what.
OK, well then what should I use, and how can I get the error message to go away !