PHP phone number parser

前端 未结 4 1062
忘了有多久
忘了有多久 2021-01-20 18:27

Building an application for UK & Ireland only but potentially it might extend to other countries. We have built an API and I\'m trying to decided how A) to store phone n

4条回答
  •  甜味超标
    2021-01-20 19:06

    Use regular expressions. An info page can be found here. It should not be too hard to learn, and will be extremely useful to you.

    Here is the regular expresssion for validating phone numbers in the United Kingdom:

    ^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$
    

    It allows 3, 4 or 5 digit regional prefix, with 8, 7 or 6 digit phone number respectively, plus optional 3 or 4 digit extension number prefixed with a # symbol. Also allows optional brackets surrounding the regional prefix and optional spaces between appropriate groups of numbers. More can be found here.

    This Stackoverflow link should help you see how regular expressions can be used with phone numbers internationally.

提交回复
热议问题