preg_match isn't working the way I want it to.

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
brocksbiz
Forum Newbie
Posts: 7
Joined: Sun Dec 05, 2010 9:28 pm

preg_match isn't working the way I want it to.

Post by brocksbiz »

I have some code.

Code: Select all

function Validation($var,$minlength,$maxlength)
{
$String_Length = strlen($var);	

$Parameter_Name = array_search($var, $GLOBALS);
print "$Parameter_Name";

$Check_String = preg_match("/[a-zA-Z0-9]{2,4}/",$var);

	if($String_Length > $minlength AND $String_Length < $maxlength)
		{
			if($var != NULL AND $Check_String == TRUE)
			{
				addslashes($var);
				return 1;
				
			} 
			else {return 2;}
		} 
		else 
		{return 3;}
		
}
Basically I'm trying to devise a way to do a general check of all the form fields that are passed into this function. preg_match() doesn't seem to be working the way it should. Does preg_match return a boolean TRUE or FALSE or what? I've tried /[A-Za-z0-9{x}/ but that doesn't seem to be working the way that it should. Please help me with this thanks.
Last edited by Benjamin on Fri Dec 17, 2010 8:16 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
brocksbiz
Forum Newbie
Posts: 7
Joined: Sun Dec 05, 2010 9:28 pm

Re: preg_match isn't working the way I want it to.

Post by brocksbiz »

I need a general regex that will check all the form fields for letters numbers - @ . and _. If someone will please explain to me how preg_match() works in not so many words it would be greatly appreciated.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: preg_match isn't working the way I want it to.

Post by Jonah Bron »

The best way to find out how preg_match behaves is to consult the manual:

http://php.net/preg-match

Referring to your second post, you want an email regex. There's information on that here: http://www.regular-expressions.info/email.html
Post Reply