Jump to content

Find postal address from post code (UK)


BCAV_WEB

Recommended Posts

Hi,

 

I've been developing my companies online site from scratch using XHTML, PHP, MySQL, Ajax etc... I decided to do this rather than a CMS system such as Joomla because I felt that they wouldn't be able to do the functions required by my boss.

 

Anyways long story short, I would like an address finder, street, house no etc... from entering a post code UK mainly, but eventually for the world, if possible as my boss has big ambitions  :P Does anyone have any ideas where I can find a script, coding, or a better yet a tutorial for this functionality.

 

ps. We don't want to pay for any pre-built systems, not bothered ab out putting an advert on our site in return for the system etc...

 

Cheers people

Link to comment
https://forums.phpfreaks.com/topic/217857-find-postal-address-from-post-code-uk/
Share on other sites

Nobody will give you a FREE street search from postcode data as in the UK this data is owned by the Royal Mail. The government did pass legislation that the data should be made open source but as of now I still don't think it is. You will have to check on the Royal Mail website.

Postcode data is constantly being updated so it is not cheap to maintain.

 

If you want proper website integration then I suggest http://www.postcodeanywhere.co.uk

 

The other option is to use Google's AJAX search APIs and link in with the maps API. However entering a postcode within Google maps does not always return a street name.

I had a quick look on Google (guessed you may look) and the first result isn't the best way of doing it. This would be better (and quicker, in most cases):

 

function validatePostcode($str)
{
    $str = strtolower($str);
    $str = str_replace(' ', '', $str);

    switch (strlen($str))
    {
        case 5:
            // A9 9AA
            return preg_match('/^[a-z][0-9][0-9][a-z]{2}$/', $str);
        case 6:
            // A99 9AA
            // AA9 9AA
            // A9A 9AA
            return preg_match('/^[a-z]([0-9]{2}|[a-z][0-9]|[a-z][0-9][a-z])[0-9][a-z]{2}$/', $str);
        case 7:
            // AA99 9AA
            // AA9A 9AA
            return preg_match('/^[a-z]{2}([0-9]{2}|[a-z][0-9])[0-9][a-z]{2}$/', $str);
    }

    return false;
}

i'm guessing $str would be the posted valued from the input form, correct? And in the return false, bit I could have something like $_SESSION['add_customer_errors']["post_code"] = "Please enter an appropriate Post Code"; right?

I'd leave the function to just validate the format of the string and return true/false, then check the return value within your main 'application' code:

 

if (!validatePostcode($postcode))
{
    $_SESSION['add_customer_errors']["post_code"] = "Please enter an appropriate Post Code";
}

 

That way you keep any of the application logic out of your functions, leaving them reusable and your code easier to follow..

Actually it would have to be, tinking about it. Just tested it and it seems to be working fine with no issues. Just a bit gutted that I can't get a address finder  :'( no chance my boss will give me the funds to buy one, he only understands the end product and expects systems done in mear minutes lol  :P

 

[

if (!validatePostcode($_SESSION['add_customer_form'] ["post_code"]))

{

    $_SESSION['add_customer_errors']["post_code"] = "Please enter an appropriate Post Code";

}

]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.