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
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.

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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";

}

]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.