I'm trying to cleanse a db field which stores UK postcodes. Postcodes can be 7 or 8 characters long but they will always have a space character in the 4th position from the end. The last 3 will be 1 digit plus 2 letters.
Customers on entering their address will frequently leave out the space (SW1A2AA instead of SW1A 2AA) so I was trying to firstly remove any space characters then re-inserting it at the 4th position from the end.
Remove the space
$query = "UPDATE db set new_postcode = replace(old_postcode, ' ' , '' ) ";$result = mysql_query($query) or die (mysql_error() . "<br />Couldn't execute query: $query");
Add the space
$new_postcode = explode(" ", $new_postcode, -3);
But I think I am not fully grasping using the explode to insert the space.