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 ?

Based on your example, I would base my parse from the right. Not the left.

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.

You are not the first person to ask this question.

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,

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}~