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


Groups > comp.lang.php > #10621

Re: CTYPE

From Norman Peelman <npeelman@cfl.rr.com>
Newsgroups comp.lang.php
Subject Re: CTYPE
Date 2013-08-25 15:11 -0400
Organization A noiseless patient Spider
Message-ID <kvdktm$ejb$1@dont-email.me> (permalink)
References <kvdg6l$cni$1@speranza.aioe.org>

Show all headers | View raw


On 08/25/2013 01:51 PM, Twayne wrote:
> Hi all,
>
> I'm still working on the postal code issue, for Canada at the moment and
> I'm still stuck.
>
> I've decided to try:
> = See if it's alpha-numeric
> = Create an array
>      = See if there is a space where it belongs
>      = Validate for digits in proper positions
>      = Since it's alpha-numeric, the 3 characters left must be Alphabetic.
>      If the validations work, keep going.
>      If not, halt & allow for a re-entry.
>
> The ctype statement will NOT work in any form and throws various errors
> regardless of wher I place it or what I use s the var. Remove the entire
> ctype, and everything works again.
>
> In code, this equates to, as best as I can:
> ---------------------------------------------------------------------------------------------------
>
> IF(strlen($zip) === 7) {
>      echo "Zip appears to be Canadian . <br />";
>      }
>      // ================== NOW LOOK TO SEE IF IT'S ALPHA-NUMERIC
>
> *    if (ctype_alnum(str_split($zip,1)) {   <---- Throws error [1]
> *    echo "Is Alpha Numeric.";
> *}
>
>              // ================ LET'S ASSIGN IT TO AN ARRAY
>          echo ' <pre>';
>        print_r(str_split($zip,1));
>        echo '</pre>';
>
>            // ================= check space where it belongs
>          if($zip[3] === " ") {
>          echo "Space in correct place<br />";
>          }
> -------------------------------------------------------------------------------------------------
>
>
> [1] : Error is:
> Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\twa1.php on
> line 107
> Line 107 is:
>      echo "Zip appears to be Canadian . <br />";
> -------------------------------------------------------------------------------------------------
>
> I have tried various forms of the ctype statement, where it's shown here
> and lower in the code after the array is generated, and using $zip[0] .
> $zip[3]... etc. I simply can not get any form of the ctype statement
> working and I have a LOT of sample code I've collected from several
> sources, all not quite what I need.
> -------------------------------------------------------------------------------------------------
>
>
> If I change the code & location as follows:
> 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)); <--------------- LINE 114
>        echo '</pre>';
>
>              if (ctype_alnum(($zip[0]))
>      echo "Is Alpha Numeric.";
>
>            // ================= check space where it belongs
>          if($zip[3] === " ") {
>          echo "Space in correct place<br />";
>          }
> ---------------------------------------------------------------------------------------------
>
> It still throws an error as:
> Parse error: syntax error, unexpected T_ECHO in C:\xampp\htdocs\twa1.php
> on line 114
>
>
> What the heck am I doing wrong? Any advice appreciated; I can't believe
> I can't get ctype to work; it seems so simple!
>
> TIA,
>
> Twayne`
>
>

   I don't understand why you're not using the regex that does away with 
all that code.

$canadian_regex = 
"/([A-CEG-HJ-NPR-TVXY]{1}[0-9]{1})[A-CEG-HJ-NPR-TVXY]{1} 
{1}([0-9]{1}[A-CEG-HJ-NPR-TVXY]{1}[0-9]{1})/";
$zip = "A2B 3X4";

IF (preg_match($canadian_regex,$zip)) {
   echo "Zip is Canadian\n";
} else {
   echo "Zip is not Canadian\n";
}

--------
or
--------


$usa_regex      = "/[0-9]{5}(-{0,1}[0-9]{4}){0,1}/";
$canadian_regex = 
"/([A-CEG-HJ-NPR-TVXY]{1}[0-9]{1})[A-CEG-HJ-NPR-TVXY]{1} 
{1}([0-9]{1}[A-CEG-HJ-NPR-TVXY]{1}[0-9]{1})/";

...

$isUSA      = (preg_match($usa_regex,$zip)) ? TRUE : FALSE;
$isCanadian = (preg_match($canadian_regex,$zip)) ? TRUE : FALSE;


--------
or
--------

// by country ISO

$postal_code['US'] = "/[0-9]{5}(-{0,1}[0-9]{4}){0,1}/";
$postal_code['CA'] = 
"/([A-CEG-HJ-NPR-TVXY]{1}[0-9]{1})[A-CEG-HJ-NPR-TVXY]{1} 
{1}([0-9]{1}[A-CEG-HJ-NPR-TVXY]{1}[0-9]{1})/";
$postal_code['VI'] = "/008[0-5]{1}1/";

$isUSA           = (preg_match($postal_code['US'],$zip)) ? TRUE : FALSE;
$isCanadian      = (preg_match($postal_code['CA'],$zip)) ? TRUE : FALSE;
$isVirginIslands = (preg_match($postal_code['VI'],$zip)) ? TRUE : FALSE;

etc.

-- 
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

Back to comp.lang.php | Previous | Next — Previous in thread | Next 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