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


Groups > comp.lang.php > #10638

Re: CTYPE THREAD RESOLVED

From Twayne <nobody@spamcop.net>
Newsgroups comp.lang.php
Subject Re: CTYPE THREAD RESOLVED
Date 2013-08-27 17:18 -0400
Organization Aioe.org NNTP Server
Message-ID <kvj528$d08$1@speranza.aioe.org> (permalink)
References <kvdg6l$cni$1@speranza.aioe.org> <kvdma1$tni$1@speranza.aioe.org> <kvg5g9$o25$1@dont-email.me>

Show all headers | View raw


On 2013-08-26 2:07 PM, Fiver wrote:
> On 2013-08-25 21:35, Twayne wrote:
>> If anyone is interested, here's the code I ended up with.  It still
>> needs to validate alphabetic & numeric array positions but that should
>> be reasonably simple to add by checking the array positions for alpha or
>> digit characters.
>>
>> Thanks for all the help!
>> -----------------------------------
>> $zip = check_input($zip);
>> $zip = strtoupper($zip);
>> // DETERMINE IF US POSTAL CODE
>> if(strlen($zip)===5) {
>> 	echo "U.S. postal code used "; }
>> 	IF (is_numeric($zip)) {
>> 	echo " and is 5 digits.";
>> 	}
>> 	IF(strlen($zip) === 7) {
>> 	echo "Zip appears to be Canadian . <br />";
>> 	}
>> 	// ================== NOW LOOK TO SEE IF IT'S ALPHA-NUMERIC
>>
>> 			// ================ LET'S ASSIGN IT TO AN ARRAY
>> 		echo ' <pre>';
>>     	print_r(str_split($zip,1));
>>     	echo '</pre>';
>> 		
>> 				  // ================= check space is where it belongs
>> 		if($zip[3] === " ") {
>> 		echo "<br />Space in correct place<br />";
>> 		}
>> 				
>> $sUser = $zip;
>> $aValid = array(' ');
>> if(!ctype_alnum(str_replace($aValid, '', $sUser))) {
>> 		echo "Username has a problem";
>>       echo 'Your Postal Code is NOT properly formatted.';
>> }
>> else {
>> echo "Postal code is properly formatted as ALPHA NUMERIC with ONE SPACE
>> as the 4th character";
>> }
>
> For the US zip codes:
>
> is_numeric() is not the correct function to use here. "+.1e1" and
> "0xCAB" are both numeric strings and would pass as US zip codes.

OOPS!  Good catch!

> Better solution:
>
>    if (strlen($zip) == 5 && ctype_digit($zip)) {
>        echo "looks like a US zip code\n";
>    }
>
> Better yet: check for known bad zip codes as well

Good point.

>
>    ... && $zip != "00000" && $zip != "99999" && I don't know, some more
>
> For the Canadian zip codes:
>
> Your code has a comment "let's assign it to an array", and you also
> wrote that you still needed to validate the array positions, but you
> never do create an array (except for the unnecessary one in $aValid,
> which should probably be a string).

Agreed; the original array code was removed at that point in the coding.

>
> Anyway, you're right that you still need to check the characters to the
> right and left of the space, or strings like "x      " and "     3 "
> would be seen as valid zip codes. Here's one way to do it:
>
>    if (strlen($zip) == 7
>        && $zip[3] == " "
>        && ctype_alpha($zip[0] . $zip[2] . $zip[5])
>        && ctype_digit($zip[1] . $zip[4] . $zip[6]))
>    {
>        echo "looks like a Canadian zip code\n";
>    }
>
> Note the two closing parens after $zip[6] ;)
>
>
> regards,
> 5er
...

Hmm, ALL great observations; thanks much.
    I'll have a look at everything you mentioned here. It's much more 
efficient than I've been putting together so far.

Regards,

Twayne`

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


Thread

CTYPE Twayne <nobody@spamcop.net> - 2013-08-25 13:51 -0400
  Re: CTYPE Richard Yates <richard@yatesguitar.com> - 2013-08-25 11:21 -0700
    Re: CTYPE Twayne <nobody@spamcop.net> - 2013-08-25 15:00 -0400
      Re: CTYPE Richard Yates <richard@yatesguitar.com> - 2013-08-25 12:05 -0700
  Re: CTYPE "J.O. Aho" <user@example.net> - 2013-08-25 20:38 +0200
    Re: CTYPE Twayne <nobody@spamcop.net> - 2013-08-25 15:28 -0400
      Re: CTYPE Richard Yates <richard@yatesguitar.com> - 2013-08-25 13:16 -0700
        Re: CTYPE Twayne <nobody@spamcop.net> - 2013-08-26 11:49 -0400
      Re: CTYPE Christoph Michael Becker <cmbecker69@arcor.de> - 2013-08-25 22:19 +0200
        Re: CTYPE Twayne <nobody@spamcop.net> - 2013-08-26 12:15 -0400
      Re: CTYPE Fiver <numeralfive@gmail.com> - 2013-08-25 22:42 +0200
        Re: CTYPE Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-26 10:25 +0200
          Re: CTYPE The Natural Philosopher <tnp@invalid.invalid> - 2013-08-26 09:29 +0100
            Re: CTYPE Twayne <nobody@spamcop.net> - 2013-08-26 12:37 -0400
  Re: CTYPE Norman Peelman <npeelman@cfl.rr.com> - 2013-08-25 15:11 -0400
    Re: CTYPE Tim Streater <timstreater@greenbee.net> - 2013-08-25 23:04 +0100
  Re: CTYPE THREAD RESOLVED Twayne <nobody@spamcop.net> - 2013-08-25 15:35 -0400
    Re: CTYPE THREAD RESOLVED Fiver <numeralfive@gmail.com> - 2013-08-26 20:07 +0200
      Re: CTYPE THREAD RESOLVED The Natural Philosopher <tnp@invalid.invalid> - 2013-08-26 20:19 +0100
        Re: CTYPE THREAD RESOLVED Fiver <numeralfive@gmail.com> - 2013-08-26 22:51 +0200
          Re: CTYPE THREAD RESOLVED Jerry Stuckle <jstucklex@attglobal.net> - 2013-08-26 16:57 -0400
            Re: CTYPE THREAD RESOLVED Twayne <nobody@spamcop.net> - 2013-08-27 17:20 -0400
              Re: CTYPE THREAD RESOLVED Jerry Stuckle <jstucklex@attglobal.net> - 2013-08-27 19:47 -0400
          Re: CTYPE THREAD RESOLVED Tim Streater <timstreater@greenbee.net> - 2013-08-26 22:07 +0100
        Re: CTYPE THREAD RESOLVED Twayne <nobody@spamcop.net> - 2013-08-28 08:49 -0400
      Re: CTYPE THREAD RESOLVED Twayne <nobody@spamcop.net> - 2013-08-27 17:18 -0400

csiph-web