#native_company# #native_desc#
#native_cta#

Get Priority Mail Zone from Zip Code

By Indy Sams
on July 28, 2002

Version: 1.0

Type: Function

Category: Shopping Carts

License: GNU General Public License

Description:
This is actually an mIRC script, but it makes a PHP file.

You can either email me your zip code ([email protected]) and I will make it for you and send it to you, or if you have knowledge of mIRC, you can follow the instructions below.

1. Goto http://postcalc.usps.gov/zonecharts/ and enter in the first three digits of your area code, and press Get Zone Chart. Then save the resulting page locally as an HTML file.

2. Open mIRC (www.mirc.com if you don’t have it), load the attached remote script, and type: /mail_convert pathtosavedchartfromstep1.html

3. It will output zones.inc.php in the folder with the HTML file.

menu menubar {
  ISB
  .Covert HTML to Zone Include File:/mail_convert $shortfn($$sfile(*.html))
}

alias mail_convert {
  set -u0 %f $1-
  set -u0 %out $nofile($1-) $+ zones.inc.php
  if (!$isfile(%f)) { halt }
  write -c %out <?
  write %out /* zones.inc.php Generated on $asctime(mm/dd/yyyy HH:nn:ssa) */
  write %out /* mail_zones.mrc script Copyright 2002 ISB/Indy Sams */
  write %out $crlf
  write %out function GetZoneFromZip($zip) $chr(123)
  write %out $crlf
  set -u0 %nn $lines(%f)
  set -u0 %n 0
  set %tot 0
  set %tott 0
  while %n < %nn {
    inc %n
    set -u0 %in $read(%f,%n)
    if (%in) {
      set -u0 %s 0
      while %s < 4 {
        inc %s

        window @buf
        ;      echo @buf %in
        if (%s = 1) { set -u0 %match <td HEADERS="HEADER1"> }
        if (%s = 2) { set -u0 %match <td HEADERS="HEADER3"> }
        if (%s = 3) { set -u0 %match <td HEADERS="HEADER5"> }
        if (%s = 4) { set -u0 %match <td HEADERS="HEADER7"> }
        set -u0 %p $pos(%in,%match,0)
        if (%p > 0) {
          set -u0 %p $pos(%in,%match,1)
          inc %p $len(%match)
          set -u0 %p2 $pos(%in,</td>,1)
          set -u0 %t $mid(%in,%p,$calc(%p2 - %p))
          set %span %t
        }

        if (%s = 1) { set -u0 %match <td HEADERS="HEADER2"> }
        if (%s = 2) { set -u0 %match <td HEADERS="HEADER4"> }
        if (%s = 3) { set -u0 %match <td HEADERS="HEADER6"> }
        if (%s = 4) { set -u0 %match <td HEADERS="HEADER8"> }
        set -u0 %p $pos(%in,%match,0)

        if (%p > 0) {
          set -u0 %p $pos(%in,%match,1)
          inc %p $len(%match)
          set -u0 %p2 $pos(%in,</td>,1)
          set -u0 %t $mid(%in,%p,$calc(%p2 - %p))
          set %zone %t
          if $right(%zone,1) = $chr(42) { set -u0 %zone $left(%zone,$calc($len(%zone) - 1)) }
          echo @buf Span: %span Zone: %zone
          echo @buf Writing Span....
          inc %tot

          if ($pos(%span,..,0) > 0) {
            set -u0 %b $left(%span,$calc($pos(%span,..,1) -1))
            set -u0 %c $right(%span,$calc($len(%span) - $pos(%span,..,1) - 1))
            echo @buf b: %b c: %c
            dec %b
            while %b < %c {
              inc %b
              inc %tott
              write %out if (substr($zip, 0, 3)  == " $+ $base(%b,10,10,3) $+ ") $chr(123) return " $+ %zone $+ "; $chr(125)
            }
          } 
          else {
            inc %tott
            write %out if (substr($zip, 0, 3) == " $+ %span $+ ") $chr(123) return " $+ %zone $+ "; $chr(125)
          }
        }

      }
    }
  }
  echo @buf Total: %tot Entries, Expanded to %tott Zones
  ;  unset %tot
  write %out $chr(125)
  write %out ?>
}


alias mail_postage {
  set -u0 %f $1-
  set -u0 %out $nofile($1-) $+ pricalc.inc.php
  if (!$isfile(%f)) { halt }
  write -c %out <?
  write %out /* pricalc.inc.php Generated on $asctime(mm/dd/yyyy HH:nn:sst) */
  write %out /* mail_zones.mrc script Copyright 2002 ISB/Indy Sams */
  write %out $crlf $+ $crlf
  write %out function CalcPriMail($lb,$zone) $chr(123) /* Input weight ($lb) and dest zone ($zone) */
  write %out $crlf $+ $crlf
  write %out $remove("$lb",") = ceil($lb,0); /* Rounds up, just like USPS */
  write %out $crlf $+ $crlf

  set -u0 %nn 71
  set -u0 %n 1
  set -u0 %lb 0
  while %n < %nn {
    inc %n
    inc %lb

    set -u0 %match *<td valign="*" headers="row $+ $base(%n,10,10,2) header2">*
    set -u0 %in $remove($read(%f,nw,%match),$remove("$","))
    if (%in) {
      write %out if ($lb == " $+ %lb $+ " && $remove("$zone",") == "1") $chr(123) return " $+ $gettok(%in,$numtok(%in,32),32) $+ "; $chr(125)
      write %out if ($lb == " $+ %lb $+ " && $remove("$zone",") == "2") $chr(123) return " $+ $gettok(%in,$numtok(%in,32),32) $+ "; $chr(125)
      write %out if ($lb == " $+ %lb $+ " && $remove("$zone",") == "3") $chr(123) return " $+ $gettok(%in,$numtok(%in,32),32) $+ "; $chr(125)
    }

    set -u0 %match *<td valign="*" headers="row $+ $base(%n,10,10,2) header3">*
    set -u0 %in $remove($read(%f,nw,%match),$remove("$","))
    if (%in) {
      write %out if ($lb == " $+ %lb $+ " && $remove("$zone",") == "4") $chr(123) return " $+ $gettok(%in,$numtok(%in,32),32) $+ "; $chr(125)
    }

    set -u0 %match *<td valign="*" headers="row $+ $base(%n,10,10,2) header4">*
    set -u0 %in $remove($read(%f,nw,%match),$remove("$","))
    if (%in) {
      write %out if ($lb == " $+ %lb $+ " && $remove("$zone",") == "5") $chr(123) return " $+ $gettok(%in,$numtok(%in,32),32) $+ "; $chr(125)
    }

    set -u0 %match *<td valign="*" headers="row $+ $base(%n,10,10,2) header5">*
    set -u0 %in $remove($read(%f,nw,%match),$remove("$","))
    if (%in) {
      write %out if ($lb == " $+ %lb $+ " && $remove("$zone",") == "6") $chr(123) return " $+ $gettok(%in,$numtok(%in,32),32) $+ "; $chr(125)
    }

    set -u0 %match *<td valign="*" headers="row $+ $base(%n,10,10,2) header6">*
    set -u0 %in $remove($read(%f,nw,%match),$remove("$","))
    if (%in) {
      write %out if ($lb == " $+ %lb $+ " && $remove("$zone",") == "7") $chr(123) return " $+ $gettok(%in,$numtok(%in,32),32) $+ "; $chr(125)
    }

    set -u0 %match *<td valign="*" headers="row $+ $base(%n,10,10,2) header7">*
    set -u0 %in $remove($read(%f,nw,%match),$remove("$","))
    if (%in) {
      write %out if ($lb == " $+ %lb $+ " && $remove("$zone",") == "8") $chr(123) return " $+ $gettok(%in,$numtok(%in,32),32) $+ "; $chr(125)
    }


  }

  write %out $chr(125)
  write %out ?>
  echo Done
}