简体   繁体   English

正则表达式PHP电话号码

[英]Regular Expression PHP Phone Number

I am making a regular expression that will read phone numbers from a PHP form. 我正在做一个正则表达式,它将从PHP表单读取电话号码。 I have the expression most of the way completed. 我的表情大部分都已完成。 It needs to read a phone number in any of the following formats: 它需要读取以下任何格式的电话号码:

 623-456-7890 456-7890 6234567890 4567890 623.456.7890 456.7890 623 456 7890 456 7890 

The expression I have made at this point is the following: 在这一点上,我的表达如下:

(([0-9]{3}){0,1})((\W){0,1})([0-9]{3})((\W){0,1})([0-9]{4})

It mostly works, the only phone number it doesn't read is the third one in the above list (6234567890) . 它可以正常工作,唯一的电话号码是上面列表中的第三个电话(6234567890) What would I have to add or change to make it read that phone number? 我需要添加或更改什么才能使其读取该电话号码?

那好多了:

([0-9]{3})?\W*([0-9]{3})\W*([0-9]{4})

/(?:[\\(]?\\d{3}[\\)\\.\\- ]?)?\\d{3}[\\.\\- ]?\\d{4}/

这是在实践中: http : //regex101.com/r/pL3dB0/3

Real phone numbers are much more complicated than this because of exchanges and so forth. 由于交换等原因,真实的电话号码要比这复杂得多。 This will match numbers, periods and hyphens, not all are required, but it also doesn't check for valid phone numbers. 这将匹配数字,句点和连字符,虽然不是全部,但也不检查有效的电话号码。

([0-9]{3})?[ .-]?([0-9]{3})[ .-]?([0-9]{4})

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

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