note 18132 deleted from function.ereg by didou
| From: | didou@php.net | Date: | Sun, 07 Dec 2003 02:58:04 +0000 |
| Subject: | note 18132 deleted from function.ereg by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-61552@lists.php.net to get a copy of this message | ||
Note Submitter: mholdgate@wakefield.co.uk
----
If you're validating/parsing British postcodes, here's some handy code
<?PHP
// Returns true if $str is a valid British postcode.
function valid_postcode ($str) {
return (ereg ('^[A-Z]{1,2}[1-9]{1,2} [1-9][A-Z]{2}$', $str));
}
// Parses a valid British postcode into it's pieces, being;
// City/Town Code
// City/Town division number
// Postcode suffix
// Results are returned in an array (See example)
function parse_postcode ($str) {
$regs = array ();
ereg ('(^[A-Z]{1,2})([1-9]{1,2}) ([1-9][A-Z]{2}$)', $str, $regs);
return ($regs);
}
$code = 'WF1 2QP';
if (valid_postcode ($code)) {
$parts = parse_postcode ($code);
echo ("$parts[0]< br>");
echo ("$parts[1]< br>");
echo ("$parts[2]< br>");
echo ("$parts[3]< br>");
echo ("$parts[4]< br>");
} else {
echo ("Invalid");
}
?>