Zip Codes ctype? Pregmatch? - Page 2

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Re: Zip Codes ctype? Pregmatch?

On 2013-08-24 6:22 AM, Curtis Dyer wrote:
Quoted text here. Click to load it
That's a great idea!


Re: Zip Codes ctype? Pregmatch?

On 2013-08-21 8:53 AM, Thomas 'PointedEars' Lahn wrote:
Quoted text here. Click to load it


Woof! A veritable cornucopia of information which I've already dedicated  
to a file on my hard drive!  I wasn't aware of most of that and it's  
going to be really handy soon's I understand it all, for now and the  

One slight correction: the Canadian valid letters are:
abc e gh jklmn p rst vxy .

Not sure where it went astray; if you need clarification visit the  
Canadian Postal Code reference; don't have the URL itself. Besides, it's  
always best to verify ANY information from any source on the 'net.

If you happen to know the Canadian system at all, the fuller breadk-down is:
$aRegion = array(
     'nl' => 'a',
     'ns' => 'b',
     'pe' => 'c',
     'nb' => 'e',
     'qc' => array('g', 'h', 'j'),
     'on' => array('k', 'l', 'm', 'n', 'p'),
     'mb' => 'r',
     'sk' => 's',
     'ab' => 't',
     'bc' => 'v',
     'nt' => 'x',
     'nu' => 'x',
     'yt' => 'y'

Also verifiable at the Canadian Postal website, including a map.

Thanks much!


Re: Zip Codes ctype? Pregmatch?

On Tue, 20 Aug 2013 13:27:50 -0400, in comp.lang.php Twayne

Quoted text here. Click to load it

Try this (I found it on the web but can't remember where) and I
haven't tired it out:

Re: Zip Codes ctype? Pregmatch?

On 2013-08-21 7:25 AM, Jeff North wrote:
Quoted text here. Click to load it

lol! I'll be happy to check it out! :)  I never did get my own to work,  
and that's a bit different from what I used.


Re: Zip Codes ctype? Pregmatch?

On 08/21/2013 06:53 PM, Twayne wrote:
Quoted text here. Click to load it

   I'd say it's a bit different... it doesn't match the rules as given.

Registered Linux user #461062
-Have you been to yet?-

Re: Zip Codes ctype? Pregmatch?

Quoted text here. Click to load it

For what purpose?  The reason I ask is that the type of checking you
do may depend on the reason for the checking.

What do you want to do with the (US) ZIP code?  (a) Mail them a
first-class letter, (b) Mail them a package, (c) Use it as verification
for the billing address on a credit card (which is about equivalent
to (a) but the credit card company does the actual mailing), (Note:
validating a credit card transaction against a payment processor
often costs real money, so it is often desirable to verify the Luhn
checksum on the credit card number, check the number of digits vs.
the bank prefix, and validate the address at least for a valid
country and state/province before sending it to the payment processor.)
(d) figure out how far it is for them to drive to one of your stores,
or (e) Use it to figure out the country since it's too hard to ask
for it?  If you're checking because some standard requires you to,
name the standard (and preferably the section).  If you're checking
because, well, er, um, I dunno, you're supposed to check user input,
aren't you?  maybe you need to review what it is you're trying to

If your requirement is to check because you want to check, you don't
care WHAT you check, you just wanna check!  You WANNA!  You WANNA!
You WANNA! then I have to wonder if the whole project should be

For the USA, there are 5 types of 5-digit ZIP codes (and you
need a database that's kept up to date for determining this):

Unassigned.  There are references to 42,000 codes being allocated
(as of 2011), so the majority of the 100,000 possible codes are
unassigned.  00000 and 99999 are permanently unassigned but only
the tip of a very large iceberg.  (Usually, unassigned codes are
not assigned a type; they are just left out of the list entirely.)

Standard.  These are geographic ZIP codes covering an area, what you'd
normally think of as a ZIP code area.

Post Office.  These are ZIP codes with an area covering only part
of the inside of a USPS Post Office (PO boxes, caller services,
etc.)  Typically a given PO Box has a unique 9-digit ZIP code.  As
a test, I once mailed a letter to "Jeff Snerfelbot" (not anywhere
close to my name) with a 9-digit zip code of a PO box.  It arrived.

Unique.  Some 5-digit ZIP codes are allocated to a single entity
that generates or receives lots of mail.  For example, Wal-Mart
Stores has 72716 and the CIA has 20505.  It is rumored that Publisher's
Clearing House has at least 2 5-digit ZIP codes, one for "YES" and
one for "NO".

Military.  These are used to route mail to US military forces,
including those stationed outside the country.

The Unassigned ZIP codes are clearly invalid.

The Standard and Unique ZIP codes are valid for most purposes.

If you are planning on using UPS or FedEx, the Post Office ZIP codes
are probably invalid, since they are filled with PO Boxes in USPS
Post Offices.

If you are planning on sending something bulky, the Military ZIP
codes may be invalid.  They may have special rules on what you can

A ZIP code may change.  I was in 2 zip code splits near Houston,
Texas around Feb. 1976 where the apartment I moved out of and the
house I moved into both changed (5-digit) ZIP codes.  9-digit
codes hadn't been put to use yet.

Private mail box services generally appear in a Standard ZIP code
and UPS or FedEx can probably deliver there if the address looks
like a street address.

A valid address doesn't avoid the possibility that (a) it's an empty
lot, and has always been an empty lot, (b) it's unoccupied, or (c)
it burned down years ago.  Someone might rebuild.  The Post Office
might eventually invalidate addresses taken over for a freeway
interchange or for which soil erosion and/or a hurricane has placed
it permanently underwater.

You probably should accept a 9-digit ZIP code as well, even if you
ignore the extra 4 digits beyond checking that they are digits,
especially if part of the purpose is country recognition.

If your requirements are to (1) accept what is or may become a valid
code in the near future, and (2) must not require periodic database
updates, then I suggest you figure out what the basic pattern is
and check that.  Be liberal in what you accept, as you can't fully
predict the future.

The USA is easy.  Rejecting 00000 and 99999 is simple, and you don't
worry about the other 57,998 or so codes that might get used in the

Unless you can find a general specification for Canadian Postal
codes, *NOT* what's currently in use, you're probably better off
allowing A-Z everywhere a letter is currently allowed.  Is there
anything around that says that an asterisk won't become part of a
valid Canadian Postal code?  or telephone number?  There are
supposedly some codes reserved for testing.  Those you can exclude.
You might also exclude H0H 0H0, which is reserved for Santa Claus.

On the other hand, if your requirements are to (1) eliminate as
many bad codes as possible, and (2) a database subscription to
issued codes is acceptable, and (3) an occasional rejection of brand
new codes is acceptable, then you want to find an API preferably
maintained by someone else to do the checking for you.

Re: Zip Codes ctype? Pregmatch?

On 2013-08-31 08:17, Gordon Burditt wrote:
Quoted text here. Click to load it
Quoted text here. Click to load it

If he had no reason to collect the postal codes at all, he wouldn't have
put them in the form (I hope). As soon as he does collect and store them
somewhere (like a database), for whatever purpose, he needs to do some
basic formal/plausibility checking.

This would be true even if he didn't limit the input to US and Canadian
codes. There are some general assumptions you can make about postal
codes that are valid everywhere in the world. For example

- no valid code will start/end with white space
- codes will never be longer than 20 characters
- codes without any alphanumeric characters are never valid

This is just basic data hygiene. I would never store user input without
at least checking the known formal constraints of the field.

The more he knows about the codes, the more he can check. This is where
the formal validation for the US/Canadian codes comes in. The general
format is known. If he has this information, he should use it, if only
to prevent some categories of typos.

Checking if a postal code is actually assigned to a real location at
this point in time, or if a certain delivery method is available to the
location - that's well outside the area of formal checks. If this level
of validation is required, he'll need an API from the delivery service
(or whoever is going to use the code). Then he needs to validate the
code at least twice: once on entry and once before sending out an actual

Quoted text here. Click to load it

Good advice.

Just wondering... why does Santa Claus have a Canadian postal code?
The geographic north pole outside Canadian territory. If his house was
as close to the north pole as he can get while still remaining on firm
land, he should have a Danish postal code.


Re: Zip Codes ctype? Pregmatch?

On 08/31/2013 10:40 AM, Fiver wrote:
Quoted text here. Click to load it

   H0H 0H0 is his Canadian mailing address... there are others.

Registered Linux user #461062
-Have you been to yet?-

Re: Zip Codes ctype? Pregmatch?

On Sat, 31 Aug 2013 11:56:32 -0400, Norman Peelman wrote:
Quoted text here. Click to load it

According to the UK Royal Mail his address is  
Santa/Father Christmas,
Santa’s Grotto,


Re: Zip Codes ctype? Pregmatch?

On 2013-08-31 11:56 AM, Norman Peelman wrote:
Quoted text here. Click to load it

True; every major city seems to have an address and a lot more just make  
up the zip codes so they seem logical to kids, and advertised locally at  
that. The post office actually just looks at who it's addressed to; if  
it's Santa, Kringle et al, it goes to the various bags they set aside  
for them. Here you can even get letters to answer yourself if there's an  
address to go along with it. They keep lists of who's 'naughty and nice'  
and it's legally the registered person's responsibility to not be stupid  
with their letters. Here there's even a form letter template that has to  
be used. There are even charity "bags" where you can anonymously  
signed-up send gifts to kids in need, and a lot more. It works neatly  
here since we're a small rural community; I don't know how other, larger  
cities work it. AFAIK there has never been a miscreant in the process;  
here, at least. Oh, and they have to be sent in special envelopes, too,  
that are donated for the purpose. I've done print runs for them several  

It reaffirms the good in people; as long as it's a full registration and  
responsibility oriented.

Cheers, won't be long & it'll be here!


Re: Zip Codes ctype? Pregmatch?

Fiver wrote:

Quoted text here. Click to load it

So that Canadian children can write to him.

Martin Leese
E-mail: please@see.Web.for.e-mail.INVALID

Re: Zip Codes ctype? Pregmatch?

On 2013-08-31 2:17 AM, Gordon Burditt wrote:
Quoted text here. Click to load it

While I certainly appreciate all the data you posted, I'm aware of most  
of it, not all, and my eventual course, which I'm getting around to now,  
is to use their API to determine valid codes (or not).

My "real" reason? Well, I've accomplished what I've set out to do for  
now and the next step is using APIs where they exist; supply a  
zip/postal code and get a response for whether it's valid or not.

Quoted text here. Click to load it

  maybe you need to review what it is you're trying to
Quoted text here. Click to load it

My goal is to learn, and what I've learned from tis thread is a great  
deal about using PHP to handle these matters for postal codes and many  
other similar formats that have nothing to do with postal codes. I  
believe I have picked up a good deal of the information/experience I  
need now, and am better off for it.


Quoted text here. Click to load it


Which is the draw to using their automated lookups to determine the  
validity or not; it's my current bent.

Quoted text here. Click to load it


They're freely available for the US and Canada; I've used them manually  
and they accomplish my goals for me. I'm very hungry for information  
from the learning PHP POV and this has been an excellent thread to that  
end. One of the better things is having learned ctype_ ... something  
I've never used successfully before and now find it an easy thing to  

Again, thanks for all that information; I appreciate a post like this  
and don't mind saying so.


Quoted text here. Click to load it

Site Timeline