Bluehost.com Web Hosting $6.95

Verifying a certain amount of numbers

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-16-2003
Ron Allen
 
Posts: n/a
Default Verifying a certain amount of numbers

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!


Reply With Quote
  #2 (permalink)  
Old 07-16-2003
Marek Kilimajer
 
Posts: n/a
Default 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!
>
>
>


Reply With Quote
  #3 (permalink)  
Old 07-16-2003
Justin French
 
Posts: n/a
Default 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 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!


Reply With Quote
  #4 (permalink)  
Old 07-16-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] Verifying a certain amount of numbers

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.
--

Reply With Quote
  #5 (permalink)  
Old 07-16-2003
Ivo Fokkema
 
Posts: n/a
Default Re: [PHP] Verifying a certain amount of numbers



"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


Reply With Quote
  #6 (permalink)  
Old 07-16-2003
Brent Baisley
 
Posts: n/a
Default 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

Reply With Quote
  #7 (permalink)  
Old 07-16-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] Verifying a certain amount of numbers

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
--


Reply With Quote
  #8 (permalink)  
Old 07-16-2003
John Manko
 
Posts: n/a
Default 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))
>>



Reply With Quote
  #9 (permalink)  
Old 07-16-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] Verifying a certain amount of numbers

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

*/
?>
Reply With Quote
  #10 (permalink)  
Old 07-17-2003
Justin French
 
Posts: n/a
Default 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

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:17 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0