note 80392 deleted from function.str-replace by felipe
| From: | felipe@php.net | Date: | Sun, 23 Mar 2008 23:06:52 +0000 |
| Subject: | note 80392 deleted from function.str-replace by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137429@lists.php.net to get a copy of this message | ||
Note Submitter: code at ashleyhunt dot co dot uk
----
I had a large volume of UK postcodes that were not formatted correctly e.g. the space was in the
wrong place or no space at all. So i wrote this simple but effective function to clean it up and
output it correctly.
<?php
function format_uk_postcode($postcode) {
// pass all legal characters excluding the space (if exists)
$postcode = ereg_replace("[^A-Za-z0-9]", "", $postcode);
// remove last 3 characters from the string leaving the first part
// (uk postcode will always end with 3 characters after the space)
$postcode_start = substr(trim($postcode), 0, -3);
// make the end part by removing the start from the postcode string
// done this way because the start can contain 2, 3 or 4 characters
$postcode_end = trim(str_replace($postcode_start, "", $postcode));
// put the start and the end together with the space in the correct place
return strtoupper($postcode_start . " " . $postcode_end);
}
?>
Hope it is of help to someone!
Ashley