This is a discussion on Verifying a certain amount of numbers within the PHP General forums, part of the PHP Programming Forums category; I would like to know how to verify that there are 6 numbers (only numbers in a field) that will ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
if(!ereg('^[0-9]{6}$',$string) die('not six numbers');
you might want to trim off white space first. Ron Allen wrote: > I would like to know how to verify that there are 6 numbers (only numbers in > a field) > that will be submitted to a database? Any clues! > > > |
|
|||
|
If you care about performance at all, try and find away around the
problem without regular expressions... I tested if( (strlen($str) == 6) && (is_int($str)) ) vs if(ereg('^[0-9]{6}$',$str)) ....on my LAN test server with 100000 iterations, and the regexp was nearly 2 times slower than the first solution. IMHO, it's also faster to read and modify the first solution too -- especially for those with minimal regexp experience. Justin On Wednesday, July 16, 2003, at 11:07 PM, Ron Allen wrote: > I would like to know how to verify that there are 6 numbers (only > numbers in > a field) > that will be submitted to a database? Any clues! |
|
|||
|
Justin French <justin@indent.com.au> wrote:
> If you care about performance at all, try and find away around the > problem without regular expressions... > > I tested > > if( (strlen($str) == 6) && (is_int($str)) ) > > vs > > if(ereg('^[0-9]{6}$',$str)) > > > ...on my LAN test server with 100000 iterations, and the regexp was > nearly 2 times slower than the first solution. IMHO, it's also faster Excellent point! I find my self using regex's a bit to often when there are other solutions available. btw, have you ever tested the difference between if(ereg('^[0-9]{6}$',$str)) if(preg_grep('^[0-9]{6}$',$str)) I've been meaning to find out advantages/disadvantages with PERL vs. POSIX compatible. Curt. -- |
|
|||
|
"Curt Zirzow" <curt@zirzow.dyndns.org> wrote in message news:20030716141706.GR50106@bagend.shire... > Excellent point! I find my self using regex's a bit to often when there > are other solutions available. > > btw, have you ever tested the difference between > > if(ereg('^[0-9]{6}$',$str)) > if(preg_grep('^[0-9]{6}$',$str)) > > I've been meaning to find out advantages/disadvantages with PERL vs. > POSIX compatible. Without getting too far astray from the real subject, I was once writing a script opening a textfile and modifying the whole contents to create a new file, using POSIX compatible expressions. My script timed out (30sec+) at some 400kb. I read Perl compatible was faster, and so I modified my script to use those. Result : done in a couple of secs. I bet it depends on the type of expression (these were a bunch of replaces) but for larger input or many iterations I'd rather use Perl compatible... -- [Win2000 | Apache/1.3.23] [PHP/4.2.3 | MySQL/3.23.53] Ivo Fokkema PHP & MySQL programmer Leiden University Medical Centre Netherlands |
|
|||
|
I'm pretty sure there will be a problem with using the is_int()
function. A similar problem was discussed a few weeks ago on this list and is_int() will return true for a "number" like 1000e2. The "e" being treated as an exponential representation of an integer. It might be more reliable to add or subtract 0 from the "string" and see if it changes. Something like: if( (strlen($str)==6) && ( strcmp(($str-0),$str)==0)) It may just be easier to use ereg if you won't be looping through a bunch of numbers. I don't know what the performance difference is between the above function and ereg or preg. On Wednesday, July 16, 2003, at 09:43 AM, Justin French wrote: > I tested > > if( (strlen($str) == 6) && (is_int($str)) ) > > vs > > if(ereg('^[0-9]{6}$',$str)) > -- Brent Baisley Systems Architect Landover Associates, Inc. Search & Advisory Services for Advanced Technology Environments p: 212.759.6400/800.759.0577 |
|
|||
|
Brent Baisley <brent@landover.com> wrote:
> I'm pretty sure there will be a problem with using the is_int() > function. A similar problem was discussed a few weeks ago on this list > and is_int() will return true for a "number" like 1000e2. The "e" being > treated as an exponential representation of an integer. I thought that was is_numeric(), I'll have to test that. > It might be more reliable to add or subtract 0 from the "string" and > see if it changes. Something like: > if( (strlen($str)==6) && ( strcmp(($str-0),$str)==0)) True that should work but it is kinda confusing code for a newbie. It doesnt really explain well what your doing. > -- > Brent Baisley > Systems Architect > Landover Associates, Inc. > Search & Advisory Services for Advanced Technology Environments > p: 212.759.6400/800.759.0577 > > Curt -- |
|
|||
|
Yes, use preg_match("/^[0-9]{6}$/",$str) since it takes care of both
number and length. You dont want to you is_numeric(), because that will match "4e5", etc..etc.. Brent Baisley wrote: > I'm pretty sure there will be a problem with using the is_int() > function. A similar problem was discussed a few weeks ago on this list > and is_int() will return true for a "number" like 1000e2. The "e" > being treated as an exponential representation of an integer. > > It might be more reliable to add or subtract 0 from the "string" and > see if it changes. Something like: > if( (strlen($str)==6) && ( strcmp(($str-0),$str)==0)) > > It may just be easier to use ereg if you won't be looping through a > bunch of numbers. I don't know what the performance difference is > between the above function and ereg or preg. > > On Wednesday, July 16, 2003, at 09:43 AM, Justin French wrote: > >> I tested >> >> if( (strlen($str) == 6) && (is_int($str)) ) >> >> vs >> >> if(ereg('^[0-9]{6}$',$str)) >> |
|
|||
|
Justin French <justin@indent.com.au> wrote:
> If you care about performance at all, try and find away around the > problem without regular expressions... > > I tested > > if( (strlen($str) == 6) && (is_int($str)) ) > I did some more tests on this problem is that $str is still considered a string and not an integer here is my test script and results: <?php // is_int() testing // standard string $str = "123"; if (is_int($str)) print "$str: yes \n"; else print "$str: no \n"; //cast to integer $str = (integer)"123"; if (is_int($str)) print "$str: yes \n"; else print "$str: no \n"; // implied cast to integer $str = "123"; $str += 0; if (is_int($str)) print "$str: yes \n"; else print "$str: no \n"; // force to be integer $str = "123"; settype($str, 'integer'); if (is_int($str)) print "$str: yes \n"; else print "$str: no \n"; /* Results: 123: no 123: yes 123: yes 123: yes */ ?> |
|
|||
|
On Thursday, July 17, 2003, at 12:17 AM, Curt Zirzow wrote:
> Excellent point! I find my self using regex's a bit to often when there > are other solutions available. > > btw, have you ever tested the difference between > > if(ereg('^[0-9]{6}$',$str)) > if(preg_grep('^[0-9]{6}$',$str)) > > I've been meaning to find out advantages/disadvantages with PERL vs. > POSIX compatible. Hi, I haven't, but feel free to do your own :) Use the sample found on http://au.php.net/microtime Justin |