Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP regex for Lebanese phone number

Tags:

regex

php

i am writing a php app that asks people for their phone number in lebanon. I don't want to be very strict in the entry format so i'm running into some trouble validating.

A lebanese phone number looks like that.

961 3 123456

961: the country code. i want it to be valid with or without it.

3: the area code. here's where it is tricky. the possible area codes are 03, 70 and 71. when the country code is present, 03 drops the 0 and becomes 3 while 70 and 71 are as is with or without country code.

123456: the phone number, always 6 digits.

here are the formats i'm trying to validate:

961 3 123456
961 70 123456
961 71 123456
03 123456
70 123456
71 123456

the spaces are here just for the sake of clarity, i am validating after stripping all spaces and non alphanumeric characters.

that's it, would be great if someone can help. thanks

like image 777
applechief Avatar asked Mar 06 '11 18:03

applechief


2 Answers

I'm sure there's a slicker way to do it, but

^(961(3|70|71)|(03|70|71))\d{6}$

seems to work, assuming I understand the requirements.

like image 167
blivet Avatar answered Nov 14 '22 03:11

blivet


^((961)?(7(0|1))|(961|0)3)[0-9]{6}$

It's a composition of these three regexes:

(961)?(7(0|1))  // 70 and 71 prefixes
(961|0)3        // (0)3 prefix
[0-9]{6}        // main number
like image 5
Czechnology Avatar answered Nov 14 '22 04:11

Czechnology