Page 1 of 1

Postcode Matching

Posted: Tue May 10, 2016 3:24 pm
by patchgill
Hi All,

I am wanting abit of advice how to go about matching postcodes together to get a value. My issue is where I have single letter postcodes against double letter prefixes and both return true.

I am not trying anything too complicated regards having ranges etc I am having postcodes either by the first letter/s or full prefixes. To give you an idea I have a product page which once the postcode is entered it checks to see if the shipping zone for this product needs to add additional prices to the total. As we are dropshipping from 6 different areas of the UK so they all different in prices.

So I have postcodes outputting in a foreach loop to check against the postcode set in a cookie. So for example I have a postcode of E1 3EE and the stored postcodes and prices are :-

E - £5
EC - £10
NW - £10
LL15 - £10
LL16 - £10
LL17 - £10

I want to loop through and find the best matched so in this case it would be E but at the moment I am getting true from 2 E and EC.

I have tried str_split to match characters with each other but I think I am way overthinking this as I have been sat having ago at this for days. Has anyone done this before or can they give me a kick in the right direction?

Your thoughts advice would be greatly appreciated.

J

Re: Postcode Matching

Posted: Tue May 10, 2016 6:44 pm
by Vegan
might be easier to use a switch as there seems to be only a few prices

pick off the special cases, rest are default

Re: Postcode Matching

Posted: Tue May 10, 2016 7:38 pm
by Christopher
Do you only care about the postcode area? If so then use preg_match() to get the initial letters (e.g. /[A-Z]*?/ ) Then lookup up the amount in a database table or array where the keys are the one or two character postcode area codes.