Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #12286

Re: Zip Codes ctype? Pregmatch? RESOLVED

Newsgroups comp.lang.php
From Moon Elf <moonelf@moonelfsystem.net>
Subject Re: Zip Codes ctype? Pregmatch? RESOLVED
References <kv08v0$614$1@speranza.aioe.org> <kv0emr$mdt$3@speranza.aioe.org> <kv0vi0$g9t$2@dont-email.me> <kv11lq$8eb$1@speranza.aioe.org>
Message-ID <slrnlatjrr.dlu.moonelf@sigtrans.org> (permalink)
Date 2013-12-16 10:02 +0000
Organization SunSITE.dk - Supporting Open source

Show all headers | View raw


On 2013-08-21, Twayne <nobody@spamcop.net> wrote:
> On 2013-08-20 7:53 PM, Norman Peelman wrote:
>> On 08/20/2013 03:06 PM, Twayne wrote:
>
> ...
>
>>>
>>
>>    I responded even though you SOLVED... at least let us know what your
>> solution was.
>>
>
> Oh, sorry; guess I was in a hurry. I simply came across a couple of 
> methods, neither of which was properly coded, and use a combo of the two 
> methods to assemble mine. If you're interested, here's a crimp sheet I 
> collected:
> --------------
>    From the Canada Post Addressing Guide:
>       "Postal codes must be printed in
>        upper case with the first three
>        elements separated from the last
>        three by one space (no hyphens)."
>
>=================================================
>
>
>   POSTAL CODES FOR 12 COUNTRIES
>
><?php
> $country_code="US";
> $zip_postal="11111";
>
> $ZIPREG=array(
> 	"US"=>"^\d{5}([\-]?\d{4})?$",
> 	"UK"=>"^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$",
> 	"DE"=>"\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b",
> 	"CA"=>"^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ 
> {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$",
> 	"FR"=>"^(F-)?((2[A|B])|[0-9]{2})[0-9]{3}$",
> 	"IT"=>"^(V-|I-)?[0-9]{5}$",
> 	"AU"=>"^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$",
> 	"NL"=>"^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$",
> 	"ES"=>"^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$",
> 	"DK"=>"^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$",
> 	"SE"=>"^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$",
> 	"BE"=>"^[1-9]{1}[0-9]{3}$"
> );
>

A faster algorithm would be to use regexes which use .+ .* with a unique
fingerprint. The above code is grinding your system probably.

I am sure tutorials such as Mastering Regular Expressions 2nd ed. would help 
out.

> if ($ZIPREG[$country_code]) {
>
> 	if (!preg_match("/".$ZIPREG[$country_code]."/i",$zip_postal)){
> 		//Validation failed, provided zip/postal code is not valid.
> 	} else {
> 		//Validation passed, provided zip/postal code is valid.
> 	}
>
> } else {
>
> 	//Validation not available
>
> }
>
>=======================================================================
> OR ...
>
>
>
>      function fnValidatePostal($mValue, $sRegion = '')
>      {
>      $mValue = strtolower($mValue));
>      $sFirst = substr($mValue, 0, 1);
>      $sRegion = strtolower($sRegion);
>
>      $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'
>      );
>
>      if (preg_match('/[abceghjlkmnprstvxy]/', $sFirst) && 
> !preg_match('/[dfioqu]/', $mValue) && preg_match('/^\w\d\w[- 
> ]?\d\w\d$/', $mValue))
>      {
>      if (!empty($sRegion) && array_key_exists($sRegion, $aRegion))
>      {
>      if (is_array($aRegion[$sRegion]) && in_array($sFirst, 
> $aRegion[$sRegion]))
>      {
>      return true;
>      }
>      else if (is_string($aRegion[$sRegion]) && $sFirst == 
> $aRegion[$sRegion])
>      {
>      return true;
>      }
>      }
>      else if (empty($sRegion))
>      {
>      return true;
>      }
>      }
>
>      return false;
>      }

Usually you do not test on vars unless you don't know at runtime what they
are which is not good coding practice.

>===================================================
> AND
>
>===========================================================
>
>      Sounds like a regexp pattern like:
>
>      Code:
>
>      /^(?:[A-CEGHJ-NPR-TVX][0-9]){3}$/
>
>      could be used to validate the form of a given Canadian postal code 
> based on the description you gave. (Whether or not the postal code is 
> truly valid/used is, of course, another matter altogether.)
>
>
>      All that being said, I see that Canada Post has an API (and I'm 
> fairly sure the USPS does, too) ... you might actually check validity 
> with the code issuing authority at the time of submission....
> ------------------------
>
>
>
> I'm most interested in using the API's that are available though as when 
> I figure out how to access them programatically I'll do so. Most of the 
> places I want to validate postal codes turn out to have an online API, 
> seriously relieving me of a lot of code and nullifying the possibility 
> of future changes, although there apparently have been few in the last 
> decade or so.
>     I'm a neophyte with little experience in these matters yet.
>
> Cheers,
>
> Twayne`

PHP is procedural if you want, which I like but you need to be clever in what
you write, procedurally or not.

ME

-- 
Member of the DR rogue circle. 
Search and you will find. 

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-20 13:27 -0400
  Re: Zip Codes  ctype? Pregmatch? Martin Leese <please@see.Web.for.e-mail.INVALID> - 2013-08-20 11:59 -0600
    Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-20 15:01 -0400
  Re: Zip Codes  ctype? Pregmatch? Robert Heller <heller@deepsoft.com> - 2013-08-20 13:08 -0500
    Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-20 15:03 -0400
      Re: Zip Codes  ctype? Pregmatch? Robert Heller <heller@deepsoft.com> - 2013-08-20 14:36 -0500
        Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-20 20:17 -0400
  Re: Zip Codes  ctype? Pregmatch? RESOLVED Twayne <nobody@spamcop.net> - 2013-08-20 15:06 -0400
    Re: Zip Codes  ctype? Pregmatch? RESOLVED Norman Peelman <npeelman@cfl.rr.com> - 2013-08-20 19:53 -0400
      Re: Zip Codes  ctype? Pregmatch? RESOLVED Twayne <nobody@spamcop.net> - 2013-08-20 20:29 -0400
        Re: Zip Codes  ctype? Pregmatch? RESOLVED Moon Elf <moonelf@moonelfsystem.net> - 2013-12-16 10:02 +0000
          Re: Zip Codes  ctype? Pregmatch? RESOLVED Doug Miller <doug_at_milmac_dot_com@example.com> - 2013-12-16 12:27 +0000
  Re: Zip Codes  ctype? Pregmatch? Norman Peelman <npeelman@cfl.rr.com> - 2013-08-20 19:52 -0400
    Re: Zip Codes  ctype? Pregmatch? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-21 14:53 +0200
      Re: Zip Codes  ctype? Pregmatch? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-21 14:58 +0200
        Re: Zip Codes  ctype? Pregmatch? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-21 15:06 +0200
          Re: Zip Codes  ctype? Pregmatch? Curtis Dyer <dyer85@gmail.com> - 2013-08-24 10:22 +0000
            Re: Zip Codes  ctype? Pregmatch? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-24 12:52 +0200
            Re: Zip Codes  ctype? Pregmatch? Doug Miller <doug_at_milmac_dot_com@example.com> - 2013-08-25 14:31 +0000
              Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-25 13:21 -0400
            Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-25 13:17 -0400
      Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-21 19:10 -0400
        Re: Zip Codes  ctype? Pregmatch? BootNic <bootnic.bounce@gmail.com> - 2013-08-21 22:09 -0400
          Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-22 18:22 -0400
  Re: Zip Codes  ctype? Pregmatch? Jeff North <jnorthau@yahoo.com.au> - 2013-08-21 21:25 +1000
    Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-21 18:53 -0400
      Re: Zip Codes  ctype? Pregmatch? Norman Peelman <npeelman@cfl.rr.com> - 2013-08-21 22:46 -0400
  Re: Zip Codes  ctype? Pregmatch? gordonb.qnlcm@burditt.org (Gordon Burditt) - 2013-08-31 01:17 -0500
    Re: Zip Codes  ctype? Pregmatch? Fiver <numeralfive@gmail.com> - 2013-08-31 16:40 +0200
      Re: Zip Codes  ctype? Pregmatch? Norman Peelman <npeelman@cfl.rr.com> - 2013-08-31 11:56 -0400
        Re: Zip Codes  ctype? Pregmatch? Welsh Vanner <welshvanners@welsh-vanners.co.uk> - 2013-08-31 20:23 +0100
        Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-31 20:21 -0400
      Re: Zip Codes  ctype? Pregmatch? Martin Leese <please@see.Web.for.e-mail.INVALID> - 2013-08-31 09:56 -0600
    Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-31 20:07 -0400

csiph-web