黎巴嫩电话号码的PHP正则表达式 [英] PHP regex for Lebanese phone number

查看:218
本文介绍了黎巴嫩电话号码的PHP正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个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 3 123456

961 :国家/地区代码.我希望有或没有它都有效.

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

3 :区号.这是棘手的地方.可能的区号是03、70和71.如果存在国家/地区代码,则03丢弃0并变成3,而使用国家/地区代码的70和71保持不变.

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 :电话号码,始终为6位数字.

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

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

推荐答案

我确定有一种轻松的方法,但是

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.

这篇关于黎巴嫩电话号码的PHP正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆