note 80392 added to function.str-replace
| From: | code at ashleyhunt dot co dot uk | Date: | Sun, 13 Jan 2008 22:37:59 +0000 |
| Subject: | note 80392 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-135080@lists.php.net to get a copy of this message | ||
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
----
Server IP: 194.145.210.3
Probable Submitter: 80.4.197.80
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/80392
Del: integrated -- https://master.php.net/note/delete/80392/integrated
Del: useless -- https://master.php.net/note/delete/80392/useless
Del: bad code -- https://master.php.net/note/delete/80392/bad+code
Del: spam -- https://master.php.net/note/delete/80392/spam
Del: non-english -- https://master.php.net/note/delete/80392/non-english
Del: in docs -- https://master.php.net/note/delete/80392/in+docs
Del: other reasons-- https://master.php.net/note/delete/80392
Reject -- https://master.php.net/note/reject/80392
Search -- https://master.php.net/manage/user-notes.php