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
hawleyjr
BeerMod
Posts: 2170 Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA
Post
by hawleyjr » Tue Sep 21, 2004 10:33 pm
I have a function that accepts a string (username) and determines if the string contains any 'forbidden' words. The code works fine. I just want to do this without using an foreach loop. Any thoughts?
Code: Select all
<?php
function checkForForbiddenWords($username){
$A_FORBIDDEN_WORDS = array('BADWORD1'=>1,
'BADWORD2'=>1,
'BADWORD3'=>1,
'BADWORD4'=>1,
'BADWORD5'=>1);
foreach($A_FORBIDDEN_WORDS as $ak => $val){
if(preg_match("/$ak/", $username))
return FALSE;
}
return TRUE;
}
?>
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Sep 22, 2004 12:15 am
compacting the array's keys (as you have them) into a matching group would work.
basically:
Code: Select all
$look = implode('|',array_keys($A_FORBIDDEN_WORDS));
return preg_match('#(' . $look . ')#i', $username);however, you have to be careful about the badwords list containing any leading or trailing spaces and regular expression symbols
phpScott
DevNet Resident
Posts: 1206 Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.
Post
by phpScott » Wed Sep 22, 2004 2:58 am
you could also use in_array()
Code: Select all
<?php
if(!in_array($username, $A_FORBIDDEN_WORDS))
echo "username is fine";
else
echo "please change username";
?>
in_array looks for the needle -> in the haystack.
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Wed Sep 22, 2004 6:11 am
phpScott wrote: you could also use in_array()
Code: Select all
<?php
if(!in_array($username, $A_FORBIDDEN_WORDS))
echo "username is fine";
else
echo "please change username";
?>
in_array looks for the needle -> in the haystack.
What is the username contains spaces, you would have to explode the username.
Preg_match is the way to go
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Wed Sep 22, 2004 8:49 am
What's wrong with a foreach loop? Just curious...
phpScott
DevNet Resident
Posts: 1206 Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.
Post
by phpScott » Wed Sep 22, 2004 8:56 am
your right Phenom.
I have only used in_array on info that I am postive about and not random user data.
hawleyjr
BeerMod
Posts: 2170 Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA
Post
by hawleyjr » Wed Sep 22, 2004 6:58 pm
Sami wrote: What's wrong with a foreach loop? Just curious...
Nothing, especially in the case I only have about 10 curse words I don't want people to use in conjunction with their username. Its one of those things where I know it can be done without a loop I just don't know how. Thanks all for the pointers.