1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with preg_match()

Discussion in 'PHP' started by chikabc, Jul 26, 2007.

  1. #1
    I have been learning php in my spare time for nearly a year now but i cannot figure out how to use preg_match to validate a phone number of 11 digits.

    Any help will be most appreciated.

    Thanks in advance
     
    chikabc, Jul 26, 2007 IP
  2. Johnston

    Johnston Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Johnston, Jul 26, 2007 IP
  3. FFMG

    FFMG Well-Known Member

    Messages:
    1,091
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    160
    #3
    Well it depends, what is the possible format?
    Numbers only?
    Spaces and numbers?

    If you look at the comment section of php, http://www.php.net/preg_match, you might see some phone number preg_match(...) that might help.

    FFMG
     
    FFMG, Jul 27, 2007 IP
    Smyrl likes this.
  4. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #4
    If you only want to validate US phone numbers, strip out all non-numeric characters, and check that the length is 11 or 10 (with/out country code).

    So:
    +1 (123) 456-7890
    Becomes:
    11234567890
    And it is 11 digits long so it is fine.

    If you need something more complex, I'd look around for a better algorithm to use. It will take too much time working out all the rules and testing them yourself.
     
    krt, Jul 27, 2007 IP
  5. chikabc

    chikabc Peon

    Messages:
    19
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for your reply so far:

    What if i ant to do simple thing like validate this sort of number: 01234567890.

    i.e 11 digits with no special characters to separate them.

    Thanks
     
    chikabc, Jul 27, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    
    
    function is_valid_number($number)
    {
        return preg_match('/^\d{11}$/', $number);
    }
    
    PHP:
    Like this?
     
    nico_swd, Jul 27, 2007 IP
  7. chikabc

    chikabc Peon

    Messages:
    19
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks everyone for your help: rep for you all
     
    chikabc, Jul 27, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    How did you know I'm German? lol
     
    nico_swd, Jul 27, 2007 IP
  9. chikabc

    chikabc Peon

    Messages:
    19
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Well, this is internet isn't it, anything can happen.

    Ok, The truth is that i was happy and i decide to speak german.

    Aufdersehen
     
    chikabc, Jul 30, 2007 IP