Re: CTYPE [message #182668 is a reply to message #182663] |
Sun, 25 August 2013 19:11 |
Norman Peelman
Messages: 126 Registered: September 2010
Karma:
|
Senior Member |
|
|
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?-
|
|
|