Regualr expressions help

Hi all,
I want to create a php regex that will accept Egyptian phone numbers (a mobile number is 11 digits) …

Egyptian phone numbers look like

0119799612
But it may be written in the following format :
011 97 99 612
011-97-99-612

Also a prefix of one of the following may be present and mayn’t

02
202
+2

Can some help ?

Hi gemswebs. Welcome to the forums. :slight_smile:

How exact do you need it to be? If you just want to check that the data includes digits, possibly some spaces, dashes and a plus, you could try something simple like this:

~^[\\+0-9\\s-]{10,20}$~

(I haven’t tested that, and regEx is not my thing.)

If you want to check for more specific patterns, you can do that too. E.g. See this thread: http://www.sitepoint.com/forums/showthread.php?808731-how-to-understand-preg_match&p=5014197#post5014197

How exact do you need it to be?

It may be any number with the format as follows :

01119799612
But it may be written in the following format :
0111 97 99 612
0111-97-99-612

Also a prefix of one of the following may be present and mayn’t

02
202
+2

But the simple regex you wrote will be very wide range , So It will match 27-12-2013

Indeed. That’s why I asked how specific it needs to be. I’m wary of trying to lock data down too tightly. I don’t see the advantage of trying to check the input in such detail, but it’s up to you.

ralph.m , Thank you for ur time…

But , I’m not trying to check inputs , I’m just trying to extract all phone numbers from uploaded CVs to my website… It’s very important for me … And it must be as accurate as possible , To match any present number with format like one of the previously mentioned numbers , and not to match any other thing !

Sure, makes sense. I’m not much good at these, but I know some others like @ScallioXTX and @ParkinT are experts. :slight_smile:

When accepting this kind of input from the user, I tend to strip all the dash characters, then process it. If the string contains any other non-number characters (such as “x” for extension), then I return the form back to the user with an error on the field.

So, basically, you would just check for a minimum of 11 digits, and a maximum of 13 digits. Then, add the area code as necessary and save it in your database as just numbers. Then when you need to display it back to the user, format the string with + and - characters as you see fit using substrings.

Or–have a drop-down field next to your phone number field. The drop-down would be a list of your country codes, and the user would just enter their actual number.

Or–have the country code number as a drop-down, and have a 3-digit field, a 2-digit field, a 2-digit field, and a 3-digit field. That would force the user to enter the phone number in a very specific format.

Hi gemswebs, welcome to the forums.

I agree with Force Flow that it is much better to control data format on input. i.e, select options vs text input.

Assuming you want to harvest old messy data - and will hopefully put new code in place to eliminate the need for resource use - pseudo-translated your string is

maybe (## or ### or +#) then (########## or ### ## ## ### or ###-##-##-###)

is this a correct assumption?

Welcome to the forums @gemswebs;
I am confident we can get you the answer(s) you need. There are a number of very talented and skilled people here who are anxious to help.

Until one of those talented and skilled folks comes along, let me try to help you.

In order to locate these numbers from a CSV list, try this RegExp:


\\+?\\d{1,2}?\\d{3}[- ]?\\d{2}[- ]?\\d{2}[- ]?\\d{3}

Based on your success with it, we may need to make some adjustments. Also, because your pattern is very complex (the number of possible ways in which the number can be displayed) it may require a multi-pass evaluation.

Thank you all for your time …

Mittineague , Force Flow , It’s not a text derived from user inputs … Wt I want to do exactly is I want to extract phone numbers from uploaded CVs’ files to y site… So I can’t control how they are written …

ParkinT … Your regular expression will not do the job accurately …

\+?\d{1,2}?\d{3}[- ]?\d{2}[- ]?\d{2}[- ]?\d{3}

This will n’t match a mobile number : 0111 111 111
A mobile number will always be 11 number … Plus spaces and digits may be scattered in where between the digits.

Any help ?

Ah, I missed that you said 11 digits in your OP and went by your examples which all have only 10 (with possiblly additional prefixes)

Can you provide more correct examples?

This might help with figuring out Egyptian phone number formats: http://en.wikipedia.org/wiki/Telephone_numbers_in_Egypt

Thank you all for your help,

More accurate examples :

0100 xxx xxxx
0101 xxx xxxx
0102 xxx xxxx
0106 xxx xxxx
0109 xxx xxxx
0111 xxx xxxx
0112 xxx xxxx
0114 xxx xxxx
0115 xxx xxxx
0120 xxx xxxx
0121 xxx xxxx
0122 xxx xxxx
0127 xxx xxxx
0128 xxx xxxx

But note that there may be prefixes or not , and spaces or dashes may be scattered anywhere between the digits , i.e. may be:
010 xx xx xxxx
OR
01 0xx xxx xxx
or any other form.

So, what makes these numbers unique from those that may appear in any other fields of the CSV? That is the key question.
A simple RegExp that searches for a string of numbers (from some minimum quantity to some maximum quantity) and ignores all dashes or spaces could be relatively simple. Would that do the trick for you?

Thank you ParkinT ,

But C.V might contain dates also like work history

IE : ( worked between: 1 10 2010 - 30 9 2011 )

Wait a second…when you say CV, do you mean Curriculum Vitae, or CSV (comma delimited file)?

Wait a second…when you say CV, do you mean Curriculum Vitae, or CSV (comma delimited file)?

Curriculum Vitae