preg_match with arrays

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

preg_match with arrays

Post by hawleyjr »

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;
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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 ;)
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

in_array

Post by phpScott »

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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: in_array

Post by John Cartwright »

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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

What's wrong with a foreach loop? Just curious...
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

your right

Post by phpScott »

your right Phenom.
I have only used in_array on info that I am postive about and not random user data.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What's wrong with feyds suggestions?
Post Reply