SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    extract all phone numbers from a text

    Hi All ,

    I want to extract all phone numbers from a text including all phone number formats.
    I mean the code must extract :
    01119799611
    00201119799611
    0111 97 99 611
    002-01119799611
    (002)01119799611
    0111.97.99.611


    Can Someone help ?

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    455
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Based on your example, I would base my parse from the right. Not the left.

  3. #3
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My example is something lile : " text text text MobileNumber text text text "

    I know that it can be done using regular expressions . But i'm pretty new to regex , So I need help creating its regex.

    I think the searched value must be : zero or more occurances of ( any number or dash or space or () or plus ) then must be : ( 01 ) then more than 9 occurances of (any number or dash or space ).

  4. #4
    SitePoint Enthusiast rajeev13's Avatar
    Join Date
    Nov 2012
    Location
    New Delhi,India
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by darroosh View Post
    My example is something lile : " text text text MobileNumber text text text "

    I know that it can be done using regular expressions . But i'm pretty new to regex , So I need help creating its regex.

    I think the searched value must be : zero or more occurances of ( any number or dash or space or () or plus ) then must be : ( 01 ) then more than 9 occurances of (any number or dash or space ).
    $regex = "^([0-9]+)?";
    preg_match($regex, $line, $match);

    where $line holds the a line from text file.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You are not the first person to ask this question.

  6. #6
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your time.

    I think I did it :
    $search = preg_match_all("/(\d+|\-|\+|\(|\)|\ ){0,}(01)(\d+|\ |\-){8,14}/",$text,$matches);

    This regex worked fine and returned all phone number but noticed two problems:
    1- if two mobile number are written beside each others , they are extracted as one number, for example: +2 0111 9799 - 618 +2 0111 9799 - 618
    2- The following is matched : "2001 - 2006 "
    Notice the spaces after 2006 , So How can I make it only one space ?
    And how can force it to stop when it finds "plus" or when it finds "tab" or "more than one space"

    Regards,

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    all them slashes.

    Lets try this a bit differently.

    How do you define the phone numbers you are attempting to find?
    From the examples you gave, I come up with the following rules:

    1. The number may begin with a 3 digit area code. This number may be encased in parenthesis, seperated by a hyphen, or not seperated at all.
    2. The number is defined to have 4 digits, followed by 2 digits, followed by 2 digits, followed by 3 digits. These digit groupings may optionally be seperated by a space or a period.

    Your pattern must reflect those two rules.
    So.
    ~(\d{3}|\(\d{3}\)|\d{3}-)?\d{4}[.\s]?\d{2}[.\s]?\d{2}[.\s]?\d{3}~
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •