DERET Pierre-Yves
DERET Pierre-Yves

Reputation: 142

Issue with PHP regex on differents type of phone numbers

I am actually stucked with a PHP Regex because I have to test different types of numbers.

I want to check if the first 2 numbers are in my array, but it can be possible that the first 0 is replaced by international numbers (+33 for France).

I don't have any idea how to test my following different types :

   +33 (0)6
   +33(0)6
   +33.6
   (+33).6
   (+33)6
   06

   +33 (0)7
   +33(0)7
   +33.7
   (+33).7
   (+33)7
   07

Any tips to do that ? Thanks

EDIT :

I tried this but not working wit parentesis :

^
   (?:(?:\+|00)33|0)    #indicatif
   \s*[6-7]             #first number (from 1 to 9)
   (?:[\s.-]*\d{2}){4}  #End of the phone number
$

Upvotes: 1

Views: 42

Answers (2)

logi-kal
logi-kal

Reputation: 7880

Try with:

$pattern = '/^(\+33( ?\(0\)|\.)|\(\+33\)\.?|0)[67][0-9]+$/';
$my_number = "+33.678910";
if(preg_match($pattern, $my_number)) // do something

If the number of digits has to be fixed, you may replace the last + with the number (or a range of numbers) that must follows the initial 6 (or 7).

For example:

/^(\+33( ?\(0\)|\.)|\(\+33\)\.?|0)[67][0-9]{5}$/
/^(\+33( ?\(0\)|\.)|\(\+33\)\.?|0)[67][0-9]{5,7}$/

The first regex means that the initial 6 (or 7) has to be followed exactly by 5 digits. The second is from 5 up to 7.

Upvotes: 1

DERET Pierre-Yves
DERET Pierre-Yves

Reputation: 142

Ok I find out I guess.

Link : Regex

$pattern = "/(?:(?:(?:\+|00)33|0)|\(\+33\))(\s*|\(0\)|\s*\(0\)|\.)[6-7](?:[\s.-]*\d{2}){4}/";

Upvotes: 1

Related Questions