Forum Moderators: coopster

Message Too Old, No Replies

Add space to postcode

need to make sure there is a space in the UK postcode

         

neonpie

3:28 pm on Oct 4, 2007 (gmt 0)

10+ Year Member



Hi All,

im pushing some xml to a clients supplier and it was failing and we tracked it down to the postcode. they need to have the postcode broken up. for example

SW1A 2AA
and not
SW1A2AA

but also dont want to end up with 2 spaces either

SW1A 2AA

i have an idea using substr and putting it into 2 variables trim any whitespace and then adding a space between the 2

Just wanted to run it by here first if anyone has a better idea or if anyone wants to knock it as a lame idea :p

thanks in advance

joelgreen

3:50 pm on Oct 4, 2007 (gmt 0)

10+ Year Member



How about removing whitespaces from post code before inserting one?

$code = str_replace(' ', '', $str);
// now add whitespace where applicable

FriskUK

6:09 pm on Oct 4, 2007 (gmt 0)

10+ Year Member



I use this

You pass it a postcode (e.g bb54hu) and it returns a properly formatted postcode (e.g. BB5 4HU)

[edited by: coopster at 2:58 pm (utc) on Oct. 8, 2007]
[edit reason] removed link to non-authoritative site [/edit]

eelixduppy

11:38 pm on Oct 4, 2007 (gmt 0)



If the above script works that's great. There are a few other ways that you can validate the postcode.

One is in the form of a regular expression. If you look at the wikipedia page for uk postcodes, they give a nice regular expression for you: [en.wikipedia.org...]

The other comes within the PEAR package. What do you know, they have a function specifically for validating uk postcodes: Validate_UK [pear.php.net].

FriskUK

9:03 am on Oct 5, 2007 (gmt 0)

10+ Year Member



The script works 100%, i use it on a leading uk property portal that demands accurate uk postcode validation and formatting :)

neonpie

12:49 pm on Oct 5, 2007 (gmt 0)

10+ Year Member



Thanks FriskUK

That function was exactly what I needed.

Cheers