reg expression to pull a phone number out of a string

Discussion in 'PHP' started by mbreezy, Feb 17, 2011.

  1. #1
    I've several strings that contains a single phone number in it in the format of 1112223333. Each string and each number is different. How would I pull this number out using regular expression? I believe the pattern would be \d{10} but I'm not completely sure on that. What function will return the single 10 digit number from the string?


    Thanks
     
    mbreezy, Feb 17, 2011 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    You already know the right way.

    Check following code..

    
    $str = 'Hi, My number is 1234567890 and i am also reachable at 4567891230';
    
    preg_match_all('/\d{10}/', $str, $matches);
    
    print_r($matches);
    
    PHP:
     
    mastermunj, Feb 17, 2011 IP
  3. mbreezy

    mbreezy Active Member

    Messages:
    135
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #3
    Excellent... I was getting all the preg functions mixed up and what not. That was the one. Thanks tons!
     
    mbreezy, Feb 18, 2011 IP