Badword Seach in String or Variable

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

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

Post by tecktalkcm0391 »

it still doesn't work any ideas?
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post by aerodromoi »

Jcart wrote:... strtolower($badwords[$i]))) ...

I would assume that is causing the error, considering your applying an array to a string function. Secondly, you might want to reconsider your whole setup as the logic and maintainability of the code seems odd. We've recently had a thread about a bbcode class which you may find of interest.
Jcart has already told you why.

Just a hint - you're trying to set a string to lowercase - so it has to be $badwords[$i][0],
not just $badwords[$i]. Btw: Why do you want to change the keys of the array? They are integers!
There is no such thing as an uppercase or lowercase number ;)

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

Post by tecktalkcm0391 »

wow I read the manual totally wrong. What I was trying to do is have the data in the array being changed all to lowercase, like if the array had Bad it would change it to bad...

Can you even do this... if so how?!?!?!?!?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

If you browse through the Array Function in the PHP Manual I am sure you will find a function that will do what you want -- plus there is plenty of example code in the manual.
(#10850)
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

ok I still cant get this to work if I try with $username = yowhatsup it doesn't work!

Code: Select all

// Username Badword Blocker
$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>";
	  print_r($bword);
	  echo "<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;
    } 
}
Any input?
Last edited by tecktalkcm0391 on Tue Jun 20, 2006 11:20 pm, edited 1 time in total.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

any mod can tell me the code to make words smurf?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

array_map() in combination with strtolower().. as abortint suggested you look for :wink:
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

this right: array_map('strtolower', $badwords);
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

this is what I have as of now...

Code: Select all

// Username Badword Blocker
$username_bad_check = $username;
$username_bad_check = strtolower($username_bad_check);
$badwords = array();
	$badwords[] = array('aSs', ' * ');
	$badwords[] = array('badword', ' * ');
// Drops * from array	
$badwords = array_map(create_function('$input', 'return array(reset($input));'), $badwords);
// Change all words to lowerchase 
$bw_count = count($badwords);
for($c=0; $c<=$bw_count; $c++){
array_map('strtolower', $badwords["$c"]);
}
// Dunno if this works:
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>";
	  print_r($bword);
	  echo "<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;
    } 
}
I get this when I run it:
Web Browser wrote:Notice: Undefined index: 2 in /home/cmmvideo/public_html/myvirtuallife.com/Login/user.php on line 124

Warning: array_map() [function.array-map]: Argument #2 should be an array in /home/cmmvideo/public_html/myvirtuallife.com/Login/user.php on line 124

Notice: Array to string conversion in /home/cmmvideo/public_html/myvirtuallife.com/Login/user.php on line 131
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.
Your username can not contain the word: Array ( [0] => aSs )



*The term "Bad Word" refeers to a curse words, an explicit words, or any other inappropriate word, or word we find to be inappropriate.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

before posting, triple check which variables you are using :? fyi, you shouldn't be using str_replace.. cough str_ireplace() for case insensitive string replaces.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

so if i use str_ireplace i wont need to worry about the upper/lower case stuff in the array or username. :P :P :P
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

nevermind. I have PHP 4.

UPDATE: I just ran phpinfo(); and my hosts updated my php version 5 :D :D :D
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

What's the point of the asterisks?

Code: Select all

$badwords[] = array('aSs', ' * ');
        $badwords[] = array('badword', ' * ');
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

its because I am using the same array as my badword filter, and I don't really feel like deleting them. Thats what

Code: Select all

$badwords = array_map(create_function('$input', 'return array(reset($input));'), $badwords);
does for me
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

Oh ok. Well here you go

Code: Select all

<?php

$username = "bass fisherman";

// Strip weird characters
$username = preg_replace('/[^A-Z0-9_]/i', '', $username);
$username = substr($username, 0, 16);

// Username Badword Blocker
$badwords = array();
$badwords[] = array('aSs', ' * ');
$badwords[] = array('badword', ' * ');

// Drops * from array
$badwords = array_map("reset", $badwords);
$badwords = array_map("preg_quote", $badwords);

// Regular expression pattern
$pattern = '/(' . implode("|", $badwords) . ')/i';

if (preg_match($pattern, $username, $matches)) {
   $match = $matches[1];

   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: $match</b>";

   echo "<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;
}

?>
Post Reply