#1
  1. No Profile Picture
    Up To No Good!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2001
    Posts
    78
    Rep Power
    14

    Match all phone numbers BUT the ones with 555 in them...


    Ok, I just spent 2 hours looking into manuals, examples, etc and still can't figure this one out... (call me slow, but it's taking me too long to figure something like this out, it's frustrating me now!)

    How do I match all phone numbers that don't use the fake 555-something number in them (so all numbers but xxx-555-xxxx will pass).

    I've got this so far:

    Code:
    ^\d{3}-\d{3}-\d{4}$
    but how do I add that 555 condition in there?

    Thanks in advance!
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    The easiest way is to apply a negative lookahead to the hyphen:
    PHP Code:
    $numbers = array("123-456-7890""212-555-1234""098-765-4321");
    foreach ( 
    $numbers AS $number ) {
        if ( 
    preg_match("/^\d{3}-(?!(555))\d{3}-\d{4}$/"$number) ) {
            echo 
    "{$number} is valid!\n";
        }

    Your pattern being:
    Code:
    ^\d{3}-(?!(555))\d{3}-\d{4}$
    -Dan

    Comments on this post

    • ishnid agrees
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Up To No Good!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2001
    Posts
    78
    Rep Power
    14
    Thank you!

    I even tried something similar to that, after reading up on the lookaheads, but didn't put the bracket around 555 and that made it unusable in my case. I was so close, yet, so far!

    Thanks again for a quick reply!
  6. #4
  7. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Originally Posted by fantom
    but didn't put the bracket around 555 and that made it unusable in my case.
    Then you must have done something else different because these two patterns match the same strings:

    Code:
    ^\d{3}-(?!(555))\d{3}-\d{4}$
    ^\d{3}-(?!555)\d{3}-\d{4}$

IMN logo majestic logo threadwatch logo seochat tools logo