简体   繁体   English

PHP正则表达式为黎巴嫩电话号码

[英]PHP regex for Lebanese phone number

i am writing a php app that asks people for their phone number in lebanon. 我正在写一个php应用程序,询问人们在黎巴嫩的电话号码。 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 3 123456

961 : the country code. 961 :国家代码。 i want it to be valid with or without it. 我想要它有或没有它是有效的。

3 : the area code. 3 :区号。 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. 可能的区号是03,70和71.当国家代码存在时,03降低0并变为3而70和71是有或没有国家代码。

123456 : the phone number, always 6 digits. 123456 :电话号码,总是6位数。

here are the formats i'm trying to validate: 这是我正在尝试验证的格式:

961 3 123456 961 3 123456
961 70 123456 961 70 123456
961 71 123456 961 71 123456
03 123456 03 123456
70 123456 70 123456
71 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 谢谢

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. 似乎工作,假设我理解要求。

^((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

Allowing for spaces or common separator chars (though I know you said you weren't concerned about them): 允许空格或常用分隔符(虽然我知道你说你不关心它们):

^((961[\s+-]*(3|7(0|1)))|(03|7(0|1)))[\s+-]*\d{6}$

http://regexr.com?2t89v http://regexr.com?2t89v

This regex handle all the area codes in lebanon https://en.wikipedia.org/wiki/Telephone_numbers_in_Lebanon 这个正则表达式处理黎巴嫩的所有区域代码https://en.wikipedia.org/wiki/Telephone_numbers_in_Lebanon

^(?:\+961|961)?(1|0?3[0-9]?|[4-6]|70|71|76|78|79|7|81?|9)\d{6}$

You can check on regexr 你可以查看regexr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM