Bad Word Censor [UNSOLVED-AGAIN]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Bad Word Censor [UNSOLVED-AGAIN]

Post 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.
Last edited by tecktalkcm0391 on Tue Jun 06, 2006 10:45 pm, edited 2 times in total.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Iterate the array and insert the spaces.

Although I would probably move the bad word list to a file/database.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

what do you mean ?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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);
User avatar
neogeek
Forum Newbie
Posts: 24
Joined: Sun May 14, 2006 4:24 am

Post 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); }
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Array problem

Post 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!
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Re: Array problem

Post 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;
    } 
}
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

THANKS!
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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 !
Post Reply