I am based in UK and I want to implement a postcode search box in my website. I have the database downloaded from http://www.easypeasy.com/guides/article.php?article=64. I have a question to ask, which I am sure is very silly for most of the experts here. If I enter a post code in my search box, how should I substring it? What I mean to say is,suppose my postcode is BS8 1RY, but there is no postcode in database as BS81RY, but BS8 is there. When I was going through the database of postcodes I found that you have 2,3 or even 4,5 digits of post code. For example, AB10 etc. and some are of 3 digits too like BS8. So, is there a standard to distinguish the postcodes and then use it in sql query!?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
This regular expression was on the UK postcode wikipedia page, it should help you identify a proper postcode:
(GIR 0AA)|(((A[BL]|B[ABDHLNRSTX]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTY]?|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)[1-9]?[0-9]|((E|N|NW|SE|SW|W)1|EC[1-4]|WC[12])[A-HJKMNPR-Y]|(SW|W)([2-9]|[1-9][0-9])|EC[1-9][0-9]) [0-9][ABD-HJLNP-UW-Z]{2})