[PHP] Verifying a certain amount of numbers

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default RE: [PHP] Verifying a certain amount of numbers

    > -----Original Message-----
    > From: Curt Zirzow [mailto:curt@zirzow.dyndns.org]
    > Sent: 16 July 2003 15:17
    >
    > 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))
    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]m.ford@lmu.ac.uk[/email]
    Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
    Mike Ford Guest

  2. Similar Questions and Discussions

    1. Verifying datasources
      Hi, everyone. I didn't write anything for a while. I have an interesting situation here and not sure what causes it and what exactly is the...
    2. Verifying form
      I have a form that I am trying to verify in PHP. If the user incorrectly filled a field, I want the form to redisplay itself with all the submitted...
    3. verifying sql INSERTs
      Hi! (MySQL) What's the best way to determine a sql INSERT statement has executed successfully? What I've got is a function whose sole...
    4. Verifying a URL
      Hi, I have situation where I want to use a stylesheet stored in one of two possible urls. Basically I need to say if the first url doesn't...
    5. 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!
  3. #2

    Default Re: [PHP] Verifying a certain amount of numbers

    Ford, Mike [LSS] <M.Ford@lmu.ac.uk> wrote:
    > > -----Original Message-----
    > > From: Curt Zirzow [mailto:curt@zirzow.dyndns.org]
    > > Sent: 16 July 2003 15:17
    > >
    > > 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))
    >
    > 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.
    Curt Zirzow Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139