php extract UK postal code and validate it

前端 未结 3 1632
误落风尘
误落风尘 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:40

    The UK Government Data Standard for postcodes is:

    ((GIR 0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY])))) [0-9][ABD-HJLNP-UW-Z]{2}))
    

    Edit: I had the above in some (personal) code with a reference to a now non-existence UK government web page. The appropriate British Standard is BS7666 and information on this is currently available here. That lists a slightly different regex.

提交回复
热议问题