Solved

how to match postal code against range of postal codes

Posted on 2010-11-11
6
447 Views
Last Modified: 2012-05-10
I have a unique problem that will probably require an expert answer.

I'm building a custom store for a canadian product and they want people from certain postal codes to not be able to ship to at all because the shipping costs are too high.

Fedex provided us with the list of postal codes in the "too far" range but they provided the data like this:

k1h > k9a
e42
j2n > l9t

so, my first though was to just print out every single possibility and stick it in a big array, not very efficient (at all)

I need a php function that can tell me whether or not the first 3 characters of the postal code are in between the start and end postal code

like

function check_postal($start,$end,$string)

it should also only check one code if start and end are the same value

can someone help me with this?
0
Comment
Question by:transitnetwork
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:jmatix
ID: 34117299
Is this what you are looking for?
<?php
function check_postal($start,$end,$string)
{
	$start = strtoupper($start);
	$end = strtoupper($end);
	$string = strtoupper($string);
	$sub = substr($string, 0, 3);
	return ($sub>=$start && $sub<=$end);
}


if (check_postal('k1h', 'k9a', 'k4a78'))
   echo "High shipping cost area";

?>

Open in new window

0
 

Author Comment

by:transitnetwork
ID: 34117304
you're on the right track but i need the code to know that it goes [a-z][1-9][a-z] with ranges...

I am looking at your code and I don't see how it could know that k4a was actually between k1h and k9a

know what I mean?
0
 

Author Comment

by:transitnetwork
ID: 34117313
Also for those not familiar with canadian postal codes and how they are structured here is a short detailed description available from the creator:
http://www.canadapost.ca/tools/pg/manual/PGaddress-e.asp#1413985
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 16

Accepted Solution

by:
jmatix earned 250 total points
ID: 34117322
PHP comparison operators can be used for lexical comparison also - meaning it will compare character by character until a difference is spotted. Then the word that has the character with the higher ascii code is considered greater than the other word.
0
 

Author Comment

by:transitnetwork
ID: 34117361
very interesting, I will test your code!
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 34122877
I copied the "too far" list verbatim from the post above.  This will get you the right answers, I think.

I didn't know Canada had 850,000 postal codes! ~Ray
<?php // RAY_temp_postal.php
error_reporting(E_ALL);
echo "<pre>";



// CHECK FOR POSTAL CODES WE DO NOT WANT http://en.wikipedia.org/wiki/Postal_codes_in_Canada
function bad_postal_code($code)
{
    // AN ARRAY OF POSTAL CODE PAIRS FROM THE SHIPPING COMPANY
    $codes = array
    ( 'k1h > k9a'
    , 'e42'
    , 'j2n > l9t'
    )
    ;

    // NORMALIZE FIRST THREE CHARACTERS TO UPPERCASE
    $test_code = strtoupper(substr(trim($code), 0, 3));

    foreach ($codes as $bad)
    {
        // NORMALIZE ALL DATA TO UPPERCASE
        $bad_array = explode(' > ', strtoupper($bad));

        // ENSURE WE HAVE A TWO-ELEMENT RANGE TO TEST
        if (!isset($bad_array[1])) $bad_array[1] = $bad_array[0];

        // RETURN TRUE IF THE POSTAL CODE IS BETWEEN THE VALUES
        if ( ($test_code >= $bad_array[0]) && ($test_code <= $bad_array[1]) ) return TRUE;
    }
    return FALSE;
}



// TEST THE FUNCTION
$c = 'e41';    if (bad_postal_code($c)) echo PHP_EOL . "$c IS BAD";
$c = 'e42';    if (bad_postal_code($c)) echo PHP_EOL . "$c IS BAD";
$c = 'j2m';    if (bad_postal_code($c)) echo PHP_EOL . "$c IS BAD";
$c = 'l8t';    if (bad_postal_code($c)) echo PHP_EOL . "$c IS BAD";
$c = 's7h4K1'; if (bad_postal_code($c)) echo PHP_EOL . "$c IS BAD";

// A DIFFERENT LOOK
$c = 'e41';    if (!bad_postal_code($c)) echo PHP_EOL . "$c IS GOOD";
$c = 'e42';    if (!bad_postal_code($c)) echo PHP_EOL . "$c IS GOOD";
$c = 'j2m';    if (!bad_postal_code($c)) echo PHP_EOL . "$c IS GOOD";
$c = 'l8t';    if (!bad_postal_code($c)) echo PHP_EOL . "$c IS GOOD";
$c = 's7h4K1'; if (!bad_postal_code($c)) echo PHP_EOL . "$c IS GOOD";

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php output utf-8 problem 6 52
How do I Import CSV File In my PHP Application 29 79
XML loaded in a form with dropdown 6 57
Horizontal Full Calendar using php 5 46
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question