简体   繁体   中英

Regular Expression PHP Phone Number

I am making a regular expression that will read phone numbers from a PHP form. 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) . 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})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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