note 63545 added to function.preg-match

From: Date: Thu, 23 Mar 2006 11:52:23 +0000
Subject: note 63545 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-106469@lists.php.net to get a copy of this message
You can also use this to verify a UK postal code, although the format is currently under a little expansion. But it goes like this: <?php if(!preg_match("/^[a-z][a-z]?\d[0-9a-z]\s?\d[a-z][a-z]$/i", $postalcode)) { echo "Your postal code is in an incorrect form."; } ?> What's the sequence? Well, just to clarify: Required letter, optional letter (specifies the area of the country), required digit, optional digit or letter (as some areas, e.g. west London have gone beyond W99 and expanded to W1T for example), then an optional space, then it requires a digit and two letters. Although this helps on a regular expression front, it does have limitations (it doesn't verify the accuracy of the postcode - e.g. A1A 0AA should match, as it is in the correct form but A is a not a valid region for postcodes) You could rewrite the first [a-z][a-z]? as a pattern matching expression where each of the valid postcode regions, e.g. B, BR, CH, CR, E and all the others are alternated - B|BR|CH|CR|E and so on. If you only run the script occasionally, it may be worth doing that. ---- Server IP: 64.71.164.2 Probable Submitter: 172.216.73.29 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- http://master.php.net/note/edit/63545 Del: integrated -- http://master.php.net/note/delete/63545/integrated Del: useless -- http://master.php.net/note/delete/63545/useless Del: bad code -- http://master.php.net/note/delete/63545/bad+code Del: spam -- http://master.php.net/note/delete/63545/spam Del: non-english -- http://master.php.net/note/delete/63545/non-english Del: in docs -- http://master.php.net/note/delete/63545/in+docs Del: other reasons-- http://master.php.net/note/delete/63545 Reject -- http://master.php.net/note/reject/63545 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#106469) next »