Re: [PHP] Verifying a certain amount of numbers
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! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Verifying a certain amount of numbers
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 10 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! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Verifying a certain amount of numbers
Justin French [EMAIL PROTECTED] 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 10 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. -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Verifying a certain amount of numbers
Curt Zirzow [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Verifying a certain amount of numbers
-Original Message- From: Curt Zirzow [mailto:[EMAIL PROTECTED] Sent: 16 July 2003 15:17 Justin French [EMAIL PROTECTED] 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 10 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)) The first will work and the second won't? Seriously, I think you mean: if (preg_match('/^[0-9]{6}$/', $str)) Having said which, a quick run on my (rather ancient and slow Windows NT) system with the above tests and 1,000,000 iterations of each reveals: Succeeding ereg: 21.1589909792 Succeeding preg_match(): 14.6125850677 Failing ereg(): 21.2370660305 Failing preg_match(): 13.5106118917 Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Verifying a certain amount of numbers
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Verifying a certain amount of numbers
Brent Baisley [EMAIL PROTECTED] 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 -- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Verifying a certain amount of numbers
Ford, Mike [LSS] [EMAIL PROTECTED] wrote: -Original Message- From: Curt Zirzow [mailto:[EMAIL PROTECTED] Sent: 16 July 2003 15:17 Justin French [EMAIL PROTECTED] 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 10 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)) The first will work and the second won't? Seriously, I think you mean: if (preg_match('/^[0-9]{6}$/', $str)) yea, I havn't used preg_* much, i wasn't aware you needed the //. Having said which, a quick run on my (rather ancient and slow Windows NT) system with the above tests and 1,000,000 iterations of each reveals: Succeeding ereg: 21.1589909792 Succeeding preg_match(): 14.6125850677 Failing ereg(): 21.2370660305 Failing preg_match(): 13.5106118917 I wonder if preg treats [0-9] vs. \d differently: if (preg_match('/^\d{6}$/', $str)) Cheers! Mike Thanks, Curt. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Verifying a certain amount of numbers
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)) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Verifying a certain amount of numbers
Justin French [EMAIL PROTECTED] 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 */ ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Verifying a certain amount of numbers
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php