regexp phone # check

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg Bryant

    regexp phone # check

    I'm working on validating US phone numbers. I have a nice expression that
    Regex Coach likes, but causes PHP to reject everything I send. Are there
    any glaring differences? I can't figure out what's wrong. Another little
    email check works fine, using the code out of Wrox' Beginning PHP.

    PhoneCheck.php
    <?php
    function PhoneCheck($num ber)
    {
    return ereg("^[(\[]?\d{3}[)-\.\] ]*\d{3}[-\. ]?\d{4}$", $number,
    $scrap);
    }
    ?>

    My test cases (which passed regex coach, but failed my php form):
    123 456 7890
    (123) 456-7890
    123.456.7890
    123-456-7890

    Thanks,
    -G
  • Pedro Graca

    #2
    Re: regexp phone # check

    Greg Bryant wrote:[color=blue]
    > I'm working on validating US phone numbers. I have a nice expression that
    > Regex Coach likes, but causes PHP to reject everything I send. Are there
    > any glaring differences? I can't figure out what's wrong. Another little
    > email check works fine, using the code out of Wrox' Beginning PHP.[/color]

    try preg_match() instead of ereg()

    and don't forget the regex delimiters
    <?php
    function PhoneCheck($num ber)
    {
    return preg_match("/^[(\[]?\d{3}[)-\.\] ]*\d{3}[-\. ]?\d{4}$/", $number, $scrap);
    }
    ?>

    --
    ..sig

    Comment

    • Greg Bryant

      #3
      Re: regexp phone # check

      Pedro Graca <hexkid@hotpop. com> wrote in
      news:bok9da$1e3 oon$1@ID-203069.news.uni-berlin.de:
      [color=blue]
      > try preg_match() instead of ereg()
      >
      > and don't forget the regex delimiters
      > <?php
      > function PhoneCheck($num ber)
      > {
      > return preg_match("/^[(\[]?\d{3}[)-\.\] ]*\d{3}[-\. ]?\d{4}$/",
      > $number, $scrap);
      > }
      > ?>
      >[/color]

      Thanks!

      Comment

      • Andy Hassall

        #4
        Re: regexp phone # check

        On Sun, 09 Nov 2003 01:34:18 GMT, Greg Bryant <bryantgHELLO@y ahoo.com> wrote:
        [color=blue]
        > return ereg("^[(\[]?\d{3}[)-\.\] ]*\d{3}[-\. ]?\d{4}$", $number,
        >$scrap);[/color]

        (Someone else has already pointed out to use preg_match instead of ereg - \d
        doesn't work in ereg)

        Put regexes inside single quotes instead of double quotes - it saves you some
        escaping.

        Inside double quotes, "\." is the same as ".", so it gets to the regex as a
        "." - match any character.

        Inside single quotes, '\.' stays as '\.', so gets to the regex as '\.' - match
        a literal '.' character.

        You don't need to escape . inside a character class anyway, so in this case it
        didnt make a difference.

        --
        Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
        Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

        Comment

        • Disco Plumber

          #5
          Re: regexp phone # check

          Andy Hassall, obviously a huge fan of Grandmaster Flash, wrote:[color=blue]
          >
          > Inside double quotes, "\." is the same as ".", so it gets to the regex as a
          > "." - match any character.[/color]

          Hmm, not in my experience...

          fnord@demogorgo n:~$ cat preg.php
          <?php
          if(preg_match(" '^\.$'", "."))
          echo "\"'^\\.$'\ " matches \".\".\n";
          if(preg_match(" '^\.$'", "x"))
          echo "\"'^\\.$'\ " matches \"x\".\n";
          ?>
          fnord@demogorgo n:~$ php4 -q preg.php
          "'^\.$'" matches ".".
          fnord@demogorgo n:~$

          /joe
          --
          In the Rich building, the decompiler is pernicious. In the beer fridge, the
          processor is fanatic... The fantastic anus is 2x-ice-cold.

          Comment

          • John Dunlop

            #6
            Re: regexp phone # check

            Andy Hassall wrote:
            [color=blue]
            > Put regexes inside single quotes instead of double quotes - it saves you some
            > escaping.[/color]

            Well, it certainly saves parsing the string for variables. But
            perhaps that may be desired from time to time.
            [color=blue]
            > Inside double quotes, "\." is the same as ".", so it gets to the regex as a
            > "." - match any character.[/color]

            I don't think so. A slash followed by a period is understood to
            mean a literal period in a regular expression, whether it's within
            single- or double-quotes, or within a character class.

            $string = 'abc';
            echo preg_match("`ab (?=\.)`",$strin g) ? 'Match' : 'No match';
            echo preg_match('`ab (?=\.)`',$strin g) ? 'Match' : 'No match';

            Moreover, in any single- or double-quoted string, a period
            following a backslash is treated literally -- that is, "\.".


            [color=blue]
            > You don't need to escape . inside a character class anyway, so in this case it
            > didnt make a difference.[/color]

            Right.

            I'm not familiar with the format(s) of US phone numbers, but, from
            the test cases provided, I suspect the hyphen must be escaped. If
            it isn't, the characters ")", "*", "+", ",", "-", ".", " ", and
            "]" will be permitted by the second character class (I.e., those
            of the ASCII repertoire between RIGHT PARENTHESIS and FULL STOP
            inclusive, and the RIGHT SQUARE BRACKET and SPACE).

            Hmm. Is "(999]...]) ]---]999 9999" really a proper national
            phone number?

            --
            Jock

            Comment

            • Andy Hassall

              #7
              Re: regexp phone # check

              On Sun, 9 Nov 2003 16:03:10 +0000 (UTC), Disco Plumber <scag@moralmino rity.org>
              wrote:
              [color=blue]
              >Andy Hassall, obviously a huge fan of Grandmaster Flash, wrote:[color=green]
              >>
              >> Inside double quotes, "\." is the same as ".", so it gets to the regex as a
              >> "." - match any character.[/color]
              >
              >Hmm, not in my experience...
              >
              > fnord@demogorgo n:~$ cat preg.php
              > <?php
              > if(preg_match(" '^\.$'", "."))
              > echo "\"'^\\.$'\ " matches \".\".\n";
              > if(preg_match(" '^\.$'", "x"))
              > echo "\"'^\\.$'\ " matches \"x\".\n";
              > ?>
              > fnord@demogorgo n:~$ php4 -q preg.php
              > "'^\.$'" matches ".".
              > fnord@demogorgo n:~$[/color]

              Whoops - my mistake.

              So ignore all my post except the bit about not needing to escape . in a
              character class.

              --
              Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
              Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

              Comment

              • Disco Plumber

                #8
                Re: regexp phone # check

                Andy Hassall (34.365% quality rating):[color=blue]
                >
                > Whoops - my mistake.
                > So ignore all my post except the bit about not needing to escape . in a
                > character class.[/color]

                I actually mentioned it to my friend, and he agreed with what you said,
                but then I showed him the code. He said it might be a version difference.
                For reference, the previous post was run with PHP 4.1.2.

                /joe
                --
                A Playstation is long. The case from Freebeer will go to El Myr!

                Comment

                • Disco Plumber

                  #9
                  Re: regexp phone # check

                  Andy Hassall (97.635% quality rating):[color=blue]
                  >
                  > So ignore all my post except the bit about not needing to escape . in a
                  > character class.[/color]

                  And you know, I actually didn't realize that, but it makes perfect
                  sense. Having a match-all character in a character class would be silly.

                  /joe
                  --
                  In the Atlanta Diner, Nick Ralabate barely felches the Student Center for
                  the anus, and then interestingly memorizes the configuration of Jon
                  Beckham's T1? In 'Narz!, Pizza says stupid shit about Q, and then triply,
                  cleverly, doubly jacks into David Wada's sig generator f... [tape runs out]

                  Comment

                  Working...