www.webdeveloper.com
Results 1 to 5 of 5

Thread: extract all phone numbers from a text

  1. #1
    Join Date
    Dec 2011
    Posts
    28

    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
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,158
    Probably you'd use preg_match_all(), but first you'll need to define all possible patterns you might want to search for, and convert them into regular expressions -- or possibly one regexp if you're lucky (or good).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    437

    php regex

    Quote Originally Posted by darroosh2 View Post
    Hi All ,

    I want to extract all phone numbers from a text including all phone number formats.
    Can Someone help ?
    You can start by learning more about regexp from places like (http://www.tutorialspoint.com/php/ph...expression.htm) and then move on to online regexp testing websites like (http://gskinner.com/RegExr/).
    Chris, Senior Developer, Chrisranjana.com
    Php Programmers

  4. #4
    Join Date
    Dec 2011
    Posts
    28
    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 ).

  5. #5
    Join Date
    Dec 2011
    Posts
    28
    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,

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles