Easy ereg() question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news@celticbear.com

    Easy ereg() question

    I'm trying to make sure a form has only one or two digits in either of
    two fields.
    I looked at php.net and
    http://www.regular-expressions.info/reference.html and this is what I
    put together, but while it successfully refuses any non-digit, it still
    lets any number of digits through.

    Any suggestions?

    if ((ereg("([0-9]{1,2})", $people))&&(ere g("([0-9]{1,2})", $rooms))) {

    Thanks!
    Liam

  • Juliette

    #2
    Re: Easy ereg() question

    news@celticbear .com wrote:[color=blue]
    > I'm trying to make sure a form has only one or two digits in either of
    > two fields.
    > I looked at php.net and
    > http://www.regular-expressions.info/reference.html and this is what I
    > put together, but while it successfully refuses any non-digit, it still
    > lets any number of digits through.
    >
    > Any suggestions?
    >
    > if ((ereg("([0-9]{1,2})", $people))&&(ere g("([0-9]{1,2})", $rooms))) {
    >
    > Thanks!
    > Liam
    >[/color]

    Try: ^([0-9]{1,2})$
    ^ = beginning of string
    $ = end of string

    Grz, jrf

    Comment

    • Mike Willbanks

      #3
      Re: Easy ereg() question

      Liam,
      [color=blue]
      > I'm trying to make sure a form has only one or two digits in either of
      > two fields.
      > I looked at php.net and
      > http://www.regular-expressions.info/reference.html and this is what I
      > put together, but while it successfully refuses any non-digit, it still
      > lets any number of digits through.
      >
      > Any suggestions?
      >
      > if ((ereg("([0-9]{1,2})", $people))&&(ere g("([0-9]{1,2})", $rooms))) {[/color]

      Instead of ereg you should always use preg_match because it is faster
      than ereg. Also part of your reason that it is matching any number of
      digit is because what you are actually asking for is to test that there
      is one or two digits in the rule...

      Try the following:

      if (preg_match('^[0-9]{1,2}$', $people) && preg_match('^[0-9]{1,2}$',
      $rooms))

      The ^ defines the start of a string and the $ defines the end of a string.

      --
      Mike Willbanks
      Zend Certified Engineer

      Comment

      • news@celticbear.com

        #4
        Re: Easy ereg() question


        Mike Willbanks wrote:[color=blue]
        > Liam,
        >
        > Instead of ereg you should always use preg_match because it is faster
        > than ereg. Also part of your reason that it is matching any number of
        > digit is because what you are actually asking for is to test that there
        > is one or two digits in the rule...
        >[/color]

        Thanks! And replier Juliette as well. Much appreciated!
        The ^ and $ are regex components that seem pretty basic, but I guess I
        completely overlooked their explanation. =/

        Thanks
        Liam

        Comment

        Working...