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

Home » Imported messages » comp.lang.php » CTYPE
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: CTYPE THREAD RESOLVED [message #182685 is a reply to message #182680] Tue, 27 August 2013 21:18 Go to previous message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma:
Senior Member
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`
[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
Previous Topic: Re: korean character sets
Next Topic: Android app Developers requirements
Goto Forum:
  

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

Current Time: Thu Nov 28 18:40:57 GMT 2024

Total time taken to generate the page: 0.04281 seconds