Page 1 of 1

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

Posted: Fri Dec 17, 2010 8:02 pm
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.

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

Posted: Fri Dec 17, 2010 8:05 pm
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.

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

Posted: Fri Dec 17, 2010 8:20 pm
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