Re: regular expresion

From: Date: Sat, 20 Oct 2001 03:41:27 +0000
Subject: Re: regular expresion
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-71652@lists.php.net to get a copy of this message
In article <20011020000605.2047.qmail@pb1.pair.com>, ragb@mail.com (Richard) wrote: > Hi, I want to validate that a variable only contains numbers. I came up with > this code > > $variable = "154545"; -> must give me true. > $variable = "$%54545"; -> must give me false > $variable = "$% 54545"; -> must give me false > $variable = "4545;#"; -> must give me false > $variable = "004545"; -> must give me false Assuming that you actually wanted true on that last one, any of the following should do: if(preg_match('/^[0-9]+$/',$variable)) if(preg_match('/^\d+$/',$variable)) if(ereg('^[0-9]+$',$variable)) if(ereg('^[[:digit:]]+$',$variable)) If you don't consider zero a valid "number", you can use either of these instead: if(preg_match('/^[1-9]+$/',$variable)) if(ereg('^[1-9]+$',$variable)) -- CC

« previous php.general (#71652) next »