note 24657 deleted from function.ereg by kennyt
| From: | kennyt@php.net | Date: | Wed, 26 Jan 2005 03:50:10 +0000 |
| Subject: | note 24657 deleted from function.ereg by kennyt | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-83975@lists.php.net to get a copy of this message | ||
Note Submitter: verdy_p at wanadoo dot fr
----
A more precise and valid parsing of British postcodes into its pieces, is:
* (1) City/Town Code (1 letter min.):
Most codes have 2 letters, some have 1 letter, possible future codes may have more letters.
* Optional (non standard) separator(s), such as space(s), used sometimes. May be stripped.
* (2) City/Town Division (required):
- 1 non-null digit exactly [*],
- followed by optional letters (currently 1 max).
Examples: "A1", "A1X", "WF1", "WF1M"
Invalid: "A12", "WF12"
* Optional separator (space or dash):
The separator is optional because of rule [*] above which limits the city code starting with a
letter to having at most 1 embedded or final digit.
The standard separator is a space, but many hand-written postcodes would not have this space very
visible, so this space must be optional and its position can be infered.
* (3) Postcode suffix (3 characters min.):
- 1 or more digits (currently 1 max.)
- optional letters (currently 2 max.)
Note that lowercase letters may be acceptable but should be avoided.
I assume that the letters I, S, and Z are avoided (to avoid confusion with the handwritten 1, 5 and
2 digits), like the digit 0 (which could be confused with the letter O).
But there may be exceptions (notably for the first letter of the code): "SI2" could be
valid, and not "S12" for the City/Town +Division Code...
A regexp that matches these rules is:
$valid = eregi(
'^[ \t]*([A-Z][A-HJ-RT-Y]?)[ \t]*-?[ \t]*([1-9][A-HJ-RT-Y]{1,2})[ \t]*-?[
\t]*([1-9][A-Z]{1,2})[ \t]*$',
$postcode, $parse);
if ($valid) {
$citycode = strtoupper($parse[0]);
$divisioncode = strtoupper($parse[1]);
$suffixcode = strtoupper($parse[2]);
}