php extract UK postal code and validate it

前端 未结 3 1635
误落风尘
误落风尘 2021-02-11 08:09

I have some text blocks like

John+and+Co-Accountants-Hove-BN31GE-2959519

I need a function to extract the postcode \"BN31GE\". It may happen to not exist

3条回答
  •  长情又很酷
    2021-02-11 08:31

    Use a regex: preg_grep function,

    I don't know the format of english postcodes but you could go with something like:

    (-[a-zA-Z0-9]+-)+
    

    This matches

    • "-Accountants-"
    • "-BN31GE-"

    You can then proceed at taking always the second value or you can enhance you regex to match exactly english postcodes, something like maybe

     ([A-Z0-9]{6})
    

提交回复
热议问题