Postcode Matching

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
patchgill
Forum Newbie
Posts: 3
Joined: Mon Feb 15, 2016 3:08 pm

Postcode Matching

Post 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
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: Postcode Matching

Post 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
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Postcode Matching

Post 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.
(#10850)
Post Reply