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
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
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 ).
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"
Bookmarks