FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Zip Codes ctype? Pregmatch?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Zip Codes ctype? Pregmatch? RESOLVED [message #184246 is a reply to message #182646] Mon, 16 December 2013 10:02 Go to previous messageGo to previous message
Moon Elf is currently offline  Moon Elf
Messages: 6
Registered: December 2013
Karma:
Junior Member
On 2013-08-21, Twayne <nobody(at)spamcop(dot)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.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Where's the error?
Next Topic: Xml Loading special Characters
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Dec 01 08:58:35 GMT 2024

Total time taken to generate the page: 0.04642 seconds