如何快速确定给定邮政编码所在的州?

How can I quickly determine the State for a given zipcode?

我不需要城市或地址,只需要州。如果可能的话,我不想打 API 电话。优先考虑的是轻量级解决方案,理想情况下只是 Javascript.

我有一个用户输入 zipcode,我想根据邮政编码所在的州显示一段文本。我知道查找城市要复杂得多 状态,为此,USPS 公开的 API 可能是最好的。但是为了匹配状态,也许只是前三个数字,解决方案(我认为)应该是简单和轻量级的。

Javascript 最理想。 PHP 也可以。

美国邮政编码数据实际上足够稳定,如果只有州(不是需要城市或其他任何东西)。

这是一个轻量级 JS 解决方案,它采用邮政编码(作为字符串),确定正确的美国州,并且 return 将州作为其两个字母的邮政缩写。

function getState(zipString) {

  /* Ensure param is a string to prevent unpredictable parsing results */
  if (typeof zipString !== 'string') {
      console.log('Must pass the zipcode as a string.');
      return;
  }

  /* Ensure we have exactly 5 characters to parse */
  if (zipString.length !== 5) {
      console.log('Must pass a 5-digit zipcode.');
      return;
  }

  /* Ensure we don't parse strings starting with 0 as octal values */
  const zipcode = parseInt(zipString, 10);

  let st;
  let state;

  /* Code cases alphabetized by state */
  if (zipcode >= 35000 && zipcode <= 36999) {
      st = 'AL';
      state = 'Alabama';
  } else if (zipcode >= 99500 && zipcode <= 99999) {
      st = 'AK';
      state = 'Alaska';
  } else if (zipcode >= 85000 && zipcode <= 86999) {
      st = 'AZ';
      state = 'Arizona';
  } else if (zipcode >= 71600 && zipcode <= 72999) {
      st = 'AR';
      state = 'Arkansas';
  } else if (zipcode >= 90000 && zipcode <= 96699) {
      st = 'CA';
      state = 'California';
  } else if (zipcode >= 80000 && zipcode <= 81999) {
      st = 'CO';
      state = 'Colorado';
  } else if ((zipcode >= 6000 && zipcode <= 6389) || (zipcode >= 6391 && zipcode <= 6999)) {
      st = 'CT';
      state = 'Connecticut';
  } else if (zipcode >= 19700 && zipcode <= 19999) {
      st = 'DE';
      state = 'Delaware';
  } else if (zipcode >= 32000 && zipcode <= 34999) {
      st = 'FL';
      state = 'Florida';
  } else if ( (zipcode >= 30000 && zipcode <= 31999) || (zipcode >= 39800 && zipcode <= 39999) ) {
      st = 'GA';
      state = 'Georgia';
  } else if (zipcode >= 96700 && zipcode <= 96999) {
      st = 'HI';
      state = 'Hawaii';
  } else if (zipcode >= 83200 && zipcode <= 83999) {
      st = 'ID';
      state = 'Idaho';
  } else if (zipcode >= 60000 && zipcode <= 62999) {
      st = 'IL';
      state = 'Illinois';
  } else if (zipcode >= 46000 && zipcode <= 47999) {
      st = 'IN';
      state = 'Indiana';
  } else if (zipcode >= 50000 && zipcode <= 52999) {
      st = 'IA';
      state = 'Iowa';
  } else if (zipcode >= 66000 && zipcode <= 67999) {
      st = 'KS';
      state = 'Kansas';
  } else if (zipcode >= 40000 && zipcode <= 42999) {
      st = 'KY';
      state = 'Kentucky';
  } else if (zipcode >= 70000 && zipcode <= 71599) {
      st = 'LA';
      state = 'Louisiana';
  } else if (zipcode >= 3900 && zipcode <= 4999) {
      st = 'ME';
      state = 'Maine';
  } else if (zipcode >= 20600 && zipcode <= 21999) {
      st = 'MD';
      state = 'Maryland';
  } else if ( (zipcode >= 1000 && zipcode <= 2799) || (zipcode == 5501) || (zipcode == 5544 ) ) {
      st = 'MA';
      state = 'Massachusetts';
  } else if (zipcode >= 48000 && zipcode <= 49999) {
      st = 'MI';
      state = 'Michigan';
  } else if (zipcode >= 55000 && zipcode <= 56899) {
      st = 'MN';
      state = 'Minnesota';
  } else if (zipcode >= 38600 && zipcode <= 39999) {
      st = 'MS';
      state = 'Mississippi';
  } else if (zipcode >= 63000 && zipcode <= 65999) {
      st = 'MO';
      state = 'Missouri';
  } else if (zipcode >= 59000 && zipcode <= 59999) {
      st = 'MT';
      state = 'Montana';
  } else if (zipcode >= 27000 && zipcode <= 28999) {
      st = 'NC';
      state = 'North Carolina';
  } else if (zipcode >= 58000 && zipcode <= 58999) {
      st = 'ND';
      state = 'North Dakota';
  } else if (zipcode >= 68000 && zipcode <= 69999) {
      st = 'NE';
      state = 'Nebraska';
  } else if (zipcode >= 88900 && zipcode <= 89999) {
      st = 'NV';
      state = 'Nevada';
  } else if (zipcode >= 3000 && zipcode <= 3899) {
      st = 'NH';
      state = 'New Hampshire';
  } else if (zipcode >= 7000 && zipcode <= 8999) {
      st = 'NJ';
      state = 'New Jersey';
  } else if (zipcode >= 87000 && zipcode <= 88499) {
      st = 'NM';
      state = 'New Mexico';
  } else if ( (zipcode >= 10000 && zipcode <= 14999) || (zipcode == 6390) || (zipcode == 501) || (zipcode == 544) ) {
      st = 'NY';
      state = 'New York';
  } else if (zipcode >= 43000 && zipcode <= 45999) {
      st = 'OH';
      state = 'Ohio';
  } else if ((zipcode >= 73000 && zipcode <= 73199) || (zipcode >= 73400 && zipcode <= 74999) ) {
      st = 'OK';
      state = 'Oklahoma';
  } else if (zipcode >= 97000 && zipcode <= 97999) {
      st = 'OR';
      state = 'Oregon';
  } else if (zipcode >= 15000 && zipcode <= 19699) {
      st = 'PA';
      state = 'Pennsylvania';
  } else if (zipcode >= 300 && zipcode <= 999) {
      st = 'PR';
      state = 'Puerto Rico';
  } else if (zipcode >= 2800 && zipcode <= 2999) {
      st = 'RI';
      state = 'Rhode Island';
  } else if (zipcode >= 29000 && zipcode <= 29999) {
      st = 'SC';
      state = 'South Carolina';
  } else if (zipcode >= 57000 && zipcode <= 57999) {
      st = 'SD';
      state = 'South Dakota';
  } else if (zipcode >= 37000 && zipcode <= 38599) {
      st = 'TN';
      state = 'Tennessee';
  } else if ( (zipcode >= 75000 && zipcode <= 79999) || (zipcode >= 73301 && zipcode <= 73399) ||  (zipcode >= 88500 && zipcode <= 88599) ) {
      st = 'TX';
      state = 'Texas';
  } else if (zipcode >= 84000 && zipcode <= 84999) {
      st = 'UT';
      state = 'Utah';
  } else if (zipcode >= 5000 && zipcode <= 5999) {
      st = 'VT';
      state = 'Vermont';
  } else if ( (zipcode >= 20100 && zipcode <= 20199) || (zipcode >= 22000 && zipcode <= 24699) || (zipcode == 20598) ) {
      st = 'VA';
      state = 'Virginia';
  } else if ( (zipcode >= 20000 && zipcode <= 20099) || (zipcode >= 20200 && zipcode <= 20599) || (zipcode >= 56900 && zipcode <= 56999) ) {
      st = 'DC';
      state = 'Washington DC';
  } else if (zipcode >= 98000 && zipcode <= 99499) {
      st = 'WA';
      state = 'Washington';
  } else if (zipcode >= 24700 && zipcode <= 26999) {
      st = 'WV';
      state = 'West Virginia';
  } else if (zipcode >= 53000 && zipcode <= 54999) {
      st = 'WI';
      state = 'Wisconsin';
  } else if (zipcode >= 82000 && zipcode <= 83199) {
      st = 'WY';
      state = 'Wyoming';
  } else {
      st = 'none';
      state = 'none';
      console.log('No state found matching', zipcode);
  }

  return st;
}

您可以 return 州的全名,而不仅仅是两个字母的缩写,方法是在最后一行 returning state 而不是 st

非常感谢@kevin-boucher 和@abaldwin99 帮助解析较小的新英格兰代码并避免可怕的八进制计算错误及其答案

还要感谢 this useful page.

的大部分原始代码

您可以 return 具有范围数组的给定邮政编码的最可能状态。

这不是验证器 - 并非范围内的每个数字实际上都指定为邮政编码,将来可能会添加新的范围。

function stateFromZip(z){
    z= parseInt(z, 10);// removes leading '0'
    if(z<1001 || z>99950) return null;
    var i= 69, next, s, 
    zs= [
        [1001, 2791, 'Massachusetts'], [2801, 2940, 'Rhode Island'], [3031, 3897, 'New Hampshire'], 
        [3901, 4992, 'Maine'], [5001, 5495, 'Vermont'], [5501, 5544, 'Massachusetts'], 
        [5601, 5907, 'Vermont'], [6001, 6389, 'Connecticut'], [6390, 6390, 'New York'], 
        [6401, 6928, 'Connecticut'], [7001, 8989, 'New Jersey'], [10001, 14975, 'New York'], 
        [15001, 19640, 'Pennsylvania'], [19701, 19980, 'Delaware'], [20001, 20039, 'Dist. of Columbia'], 
        [20040, 20167, 'Virginia'], [20042, 20599, 'Dist. of Columbia'], [20331, 20331, 'Maryland'], 
        [20335, 20797, 'Maryland'], [20799, 20799, 'Dist. of Columbia'], [20812, 21930, 'Maryland'], 
        [22001, 24658, 'Virginia'], [24701, 26886, 'West Virginia'], [27006, 28909, 'North Carolina'], 
        [29001, 29948, 'South Carolina'], [30001, 31999, 'Georgia'], [32004, 34997, 'Florida'], 
        [35004, 36925, 'Alabama'], [37010, 38589, 'Tennessee'], [38601, 39776, 'Mississippi'], 
        [39901, 39901, 'Georgia'], [40003, 42788, 'Kentucky'], [43001, 45999, 'Ohio'], 
        [46001, 47997, 'Indiana'], [48001, 49971, 'Michigan'], [50001, 52809, 'Iowa'], 
        [53001, 54990, 'Wisconsin'], [55001, 56763, 'Minnesota'], [57001, 57799, 'South Dakota'], 
        [58001, 58856, 'North Dakota'], [59001, 59937, 'Montana'], [60001, 62999, 'Illinois'], 
        [63001, 65899, 'Missouri'], [66002, 67954, 'Kansas'], [68001, 68118, 'Nebraska'], 
        [68119, 68120, 'Iowa'], [68122, 69367, 'Nebraska'], [70001, 71232, 'Louisiana'], 
        [71233, 71233, 'Mississippi'], [71234, 71497, 'Louisiana'], [73001, 73199, 'Oklahoma'], 
        [73301, 73301, 'Texas'], [73401, 74966, 'Oklahoma'], [75001, 75501, 'Texas'], 
        [75502, 75502, 'Arkansas'], [75503, 79999, 'Texas'], [80001, 81658, 'Colorado'], 
        [82001, 83128, 'Wyoming'], [83201, 83876, 'Idaho'], [84001, 84784, 'Utah'], 
        [85001, 86556, 'Arizona'], [87001, 88441, 'New Mexico'], [88510, 88589, 'Texas'], 
        [88901, 89883, 'Nevada'], [90001, 96162, 'California'], [96701, 96898, 'Hawaii'], 
        [97001, 97920, 'Oregon'], [98001, 99403, 'Washington'], [99501, 99950, 'Alaska']
    ];

    while(i){
        next= zs[--i];
        if(z>next[0] && z<next[1]) return next[2];
    }
    return null;
}

stateFromZip('49125')

/* returned 值:(字符串) 密歇根州 */

zippopotam.us 有一个 REST API。 这是一个如何从纯 JavaScript(无库)的 ZIP 中获取状态的示例:

var getStatebutton = document.getElementById('GetStateButton');

getStatebutton.onclick = function () {
    var zipCode = document.getElementById('ZIPCode');
    var zip = zipCode.value;
    if (!zip) return;
    var url = 'http://api.zippopotam.us/us/' + zip;
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            var result = xhr.responseText;
            var zippo = JSON.parse(result);
            var resultDiv = document.getElementById('divResult');
            resultDiv.innerHTML = zippo.places[0].state;
        }
    };
    xhr.open('GET', url, true);
    xhr.send(null);
};
ZIP:
<input type='text' id='ZIPCode' value='90210' />
<button id="GetStateButton">Get State</button>
<p></p>State:
<div id='divResult'></div>

jsfiddle 如果您愿意。

function getState(zipcode) {
    // Returns false on invalid zip-- else returns {code:"XX" long:"XXXXXXXXX"}

    // Ensure param is a string to prevent unpredictable parsing results
    if (typeof zipcode !== 'string') {
        console.log('Must pass the zipcode as a string. -- Otherwise leading zeros could cause your zip code to be parsed outside base 10.');
        return;
    }

    // Ensure you don't parse codes that start with 0 as octal values
    zipcode = parseInt(zipcode,10);

    // Code blocks alphabetized by state
    var states = [{min: 35000, max:36999, code: 'AL', long: "Alabama"},
    {min: 99500, max:99999, code: 'AK', long: "Alaska"},
    {min: 85000, max:86999, code: 'AZ', long: "Arizona"},
    {min: 71600, max:72999, code: 'AR', long: "Arkansas"},
    {min: 90000, max:96699, code: 'CA', long: "California"},
    {min: 80000, max:81999, code: 'CO', long: "Colorado"},
    {min: 6000,  max:6999,  code: 'CT', long: "Connecticut"},
    {min: 19700, max:19999, code: 'DE', long: "Deleware"},
    {min: 32000, max:34999, code: 'FL', long: "Florida"},
    {min: 30000, max:31999, code: 'GA', long: "Georgia"},
    {min: 96700, max:96999, code: 'HI', long: "Hawaii"},
    {min: 83200, max:83999, code: 'ID', long: "Idaho"},
    {min: 60000, max:62999, code: 'IL', long: "Illinois"},
    {min: 46000, max:47999, code: 'IN', long: "Indiana"},
    {min: 50000, max:52999, code: 'IA', long: "Iowa"},
    {min: 66000, max:67999, code: 'KS', long: "Kansas"},
    {min: 40000, max:42999, code: 'KY', long: "Kentucky"},
    {min: 70000, max:71599, code: 'LA', long: "Louisiana"},
    {min: 3900,  max:4999,  code: 'ME', long: "Maine"},
    {min: 20600, max:21999, code: 'MD', long: "Maryland"},
    {min: 1000,  max:2799,  code: 'MA', long: "Massachusetts"},
    {min: 48000, max:49999, code: 'MI', long: "Michigan"},
    {min: 55000, max:56999, code: 'MN', long: "Minnesota"},
    {min: 38600, max:39999, code: 'MS', long: "Mississippi"},
    {min: 63000, max:65999, code: 'MO', long: "Missouri"},
    {min: 59000, max:59999, code: 'MT', long: "Montana"},
    {min: 27000, max:28999, code: 'NC', long: "North Carolina"},
    {min: 58000, max:58999, code: 'ND', long: "North Dakota"},
    {min: 68000, max:69999, code: 'NE', long: "Nebraska"},
    {min: 88900, max:89999, code: 'NV', long: "Nevada"},
    {min: 3000, max:3899, code: 'NH', long: "New Hampshire"},
    {min: 7000, max:8999, code: 'NJ', long: "New Jersey"},
    {min: 87000, max:88499, code: 'NM', long: "New Mexico"},
    {min: 10000, max:14999, code: 'NY', long: "New York"},
    {min: 43000, max:45999, code: 'OH', long: "Ohio"},
    {min: 73000, max:74999, code: 'OK', long: "Oklahoma"},
    {min: 97000, max:97999, code: 'OR', long: "Oregon"},
    {min: 15000, max:19699, code: 'PA', long: "Pennsylvania"},
    {min: 300, max:999, code: 'PR', long: "Puerto Rico"},
    {min: 2800, max:2999, code: 'RI', long: "Rhode Island"},
    {min: 29000, max:29999, code: 'SC', long: "South Carolina"},
    {min: 57000, max:57999, code: 'SD', long: "South Dakota"},
    {min: 37000, max:38599, code: 'TN', long: "Tennessee"},
    {min: 75000, max:79999, code: 'TX', long: "Texas"},
    {min: 88500, max:88599, code: 'TX', long: "Texas"},
    {min: 84000, max:84999, code: 'UT', long: "Utah"},
    {min: 5000, max:5999, code: 'VT', long: "Vermont"},
    {min: 22000, max:24699, code: 'VA', long: "Virgina"},
    {min: 20000, max:20599, code: 'DC', long: "Washington DC"},
    {min: 98000, max:99499, code: 'WA', long: "Washington"},
    {min: 24700, max:26999, code: 'WV', long: "West Virginia"},
    {min: 53000, max:54999, code: 'WI', long: "Wisconsin"},
    {min: 82000, max:83199, code: 'WY', long: "Wyoming"}];

    var state = states.filter(function(s){
        return s.min <= zipcode && s.max >= zipcode;        
    });

    if (state.length == 0){
        return false;
    } else if (state.length > 1) {
        console.error("Whoops found two states");
    }
    return {code:state[0].code, long:state[0].long};

}

我意识到 OP 最初要求 PHP 或 JS,但我最终来到这里,所以其他人可能也会发现这很有用。

这是一个更新了邮政编码范围的 C# 版本(从 42k 邮政编码的数据库构建)

public static string GetStateFromZip(int zipCode) {
    return zipCode switch {
        var zip when zip >= 600 && zip <= 999 => "Puerto Rico",
        var zip when zip >= 1001 && zip <= 2791 => "Massachusetts",
        var zip when zip >= 2801 && zip <= 2940 => "Rhode Island",
        var zip when zip >= 3031 && zip <= 3897 => "New Hampshire",
        var zip when zip >= 3901 && zip <= 4992 => "Maine",
        var zip when zip >= 5001 && zip <= 5495 => "Vermont",
        var zip when zip >= 5501 && zip <= 5544 => "Massachusetts",
        var zip when zip >= 5601 && zip <= 5907 => "Vermont",
        var zip when zip >= 6001 && zip <= 6389 => "Connecticut",
        var zip when zip >= 6390 && zip <= 6390 => "New York",
        var zip when zip >= 6401 && zip <= 6928 => "Connecticut",
        var zip when zip >= 7001 && zip <= 8989 => "New Jersey",
        var zip when zip >= 9000 && zip <= 9900 => "Military",
        var zip when zip >= 10001 && zip <= 14975 => "New York",
        var zip when zip >= 15001 && zip <= 19640 => "Pennsylvania",
        var zip when zip >= 19701 && zip <= 19980 => "Delaware",
        var zip when zip >= 20001 && zip <= 20039 => "Washington, D.C.",
        var zip when zip >= 20040 && zip <= 20167 => "Virginia",
        var zip when zip >= 20042 && zip <= 20599 => "Washington, D.C.",
        var zip when zip >= 20331 && zip <= 20331 => "Maryland",
        var zip when zip >= 20335 && zip <= 20797 => "Maryland",
        var zip when zip >= 20799 && zip <= 20799 => "Washington, D.C.",
        var zip when zip >= 20810 && zip <= 21930 => "Maryland",
        var zip when zip >= 22001 && zip <= 24658 => "Virginia",
        var zip when zip >= 24701 && zip <= 26886 => "West Virginia",
        var zip when zip >= 27006 && zip <= 28909 => "North Carolina",
        var zip when zip >= 29001 && zip <= 29948 => "South Carolina",
        var zip when zip >= 30001 && zip <= 31999 => "Georgia",
        var zip when zip >= 32000 && zip <= 34997 => "Florida",
        var zip when zip >= 35004 && zip <= 36925 => "Alabama",
        var zip when zip >= 37010 && zip <= 38589 => "Tennessee",
        var zip when zip >= 38601 && zip <= 39776 => "Mississippi",
        var zip when zip >= 39813 && zip <= 39897 => "Georgia",
        var zip when zip >= 39901 && zip <= 39901 => "Georgia",
        var zip when zip >= 40003 && zip <= 42788 => "Kentucky",
        var zip when zip >= 43001 && zip <= 45999 => "Ohio",
        var zip when zip >= 46001 && zip <= 47997 => "Indiana",
        var zip when zip >= 48001 && zip <= 49971 => "Michigan",
        var zip when zip >= 50001 && zip <= 52809 => "Iowa",
        var zip when zip >= 53001 && zip <= 54990 => "Wisconsin",
        var zip when zip >= 55001 && zip <= 56763 => "Minnesota",
        var zip when zip >= 56901 && zip <= 56999 => "Washington, D.C.",
        var zip when zip >= 57001 && zip <= 57799 => "South Dakota",
        var zip when zip >= 58001 && zip <= 58856 => "North Dakota",
        var zip when zip >= 59001 && zip <= 59937 => "Montana",
        var zip when zip >= 60001 && zip <= 62999 => "Illinois",
        var zip when zip >= 63001 && zip <= 65899 => "Missouri",
        var zip when zip >= 66002 && zip <= 67954 => "Kansas",
        var zip when zip >= 68001 && zip <= 68118 => "Nebraska",
        var zip when zip >= 68119 && zip <= 68120 => "Iowa",
        var zip when zip >= 68122 && zip <= 69367 => "Nebraska",
        var zip when zip >= 70001 && zip <= 71232 => "Louisiana",
        var zip when zip >= 71233 && zip <= 71233 => "Mississippi",
        var zip when zip >= 71234 && zip <= 71497 => "Louisiana",
        var zip when zip >= 71601 && zip <= 72959 => "Arkansas",
        var zip when zip >= 73001 && zip <= 73199 => "Oklahoma",
        var zip when zip >= 73301 && zip <= 73399 => "Texas",
        var zip when zip >= 73401 && zip <= 74966 => "Oklahoma",
        var zip when zip >= 75001 && zip <= 75501 => "Texas",
        var zip when zip >= 75502 && zip <= 75502 => "Arkansas",
        var zip when zip >= 75503 && zip <= 79999 => "Texas",
        var zip when zip >= 80001 && zip <= 81658 => "Colorado",
        var zip when zip >= 82001 && zip <= 83128 => "Wyoming",
        var zip when zip >= 83201 && zip <= 83876 => "Idaho",
        var zip when zip >= 84001 && zip <= 84790 => "Utah",
        var zip when zip >= 85001 && zip <= 86556 => "Arizona",
        var zip when zip >= 87001 && zip <= 88441 => "New Mexico",
        var zip when zip >= 88510 && zip <= 88595 => "Texas",
        var zip when zip >= 88901 && zip <= 89883 => "Nevada",
        var zip when zip >= 90001 && zip <= 96162 => "California",
        var zip when zip >= 96201 && zip <= 96698 => "Military",
        var zip when zip >= 96701 && zip <= 96898 => "Hawaii",
        var zip when zip >= 96910 && zip <= 96932 => "Guam",
        var zip when zip >= 97001 && zip <= 97920 => "Oregon",
        var zip when zip >= 98001 && zip <= 99403 => "Washington",
        var zip when zip >= 99501 && zip <= 99950 => "Alaska",
        _ => ""
    };
}

这是@Tony Brasunas 接受的答案PHP 版本

<?php

function getState( $zipcode ) {

  /* Ensure we have exactly 5 characters to parse */
  if ( strlen( $zipcode ) !== 5) {
      return 'Must pass a 5-digit $zipcode.';
  }

  $st = '';
  $state = '';

  /* Code cases alphabetized by state */
  if ($zipcode >= 35000 && $zipcode <= 36999) {
      $st = 'AL';
      $state = 'Alabama';
  } else if ($zipcode >= 99500 && $zipcode <= 99999) {
      $st = 'AK';
      $state = 'Alaska';
  } else if ($zipcode >= 85000 && $zipcode <= 86999) {
      $st = 'AZ';
      $state = 'Arizona';
  } else if ($zipcode >= 71600 && $zipcode <= 72999) {
      $st = 'AR';
      $state = 'Arkansas';
  } else if ($zipcode >= 90000 && $zipcode <= 96699) {
      $st = 'CA';
      $state = 'California';
  } else if ($zipcode >= 80000 && $zipcode <= 81999) {
      $st = 'CO';
      $state = 'Colorado';
  } else if (($zipcode >= 6000 && $zipcode <= 6389) || ($zipcode >= 6391 && $zipcode <= 6999)) {
      $st = 'CT';
      $state = 'Connecticut';
  } else if ($zipcode >= 19700 && $zipcode <= 19999) {
      $st = 'DE';
      $state = 'Delaware';
  } else if ($zipcode >= 32000 && $zipcode <= 34999) {
      $st = 'FL';
      $state = 'Florida';
  } else if ( ($zipcode >= 30000 && $zipcode <= 31999) || ($zipcode >= 39800 && $zipcode <= 39999) ) {
      $st = 'GA';
      $state = 'Georgia';
  } else if ($zipcode >= 96700 && $zipcode <= 96999) {
      $st = 'HI';
      $state = 'Hawaii';
  } else if ($zipcode >= 83200 && $zipcode <= 83999) {
      $st = 'ID';
      $state = 'Idaho';
  } else if ($zipcode >= 60000 && $zipcode <= 62999) {
      $st = 'IL';
      $state = 'Illinois';
  } else if ($zipcode >= 46000 && $zipcode <= 47999) {
      $st = 'IN';
      $state = 'Indiana';
  } else if ($zipcode >= 50000 && $zipcode <= 52999) {
      $st = 'IA';
      $state = 'Iowa';
  } else if ($zipcode >= 66000 && $zipcode <= 67999) {
      $st = 'KS';
      $state = 'Kansas';
  } else if ($zipcode >= 40000 && $zipcode <= 42999) {
      $st = 'KY';
      $state = 'Kentucky';
  } else if ($zipcode >= 70000 && $zipcode <= 71599) {
      $st = 'LA';
      $state = 'Louisiana';
  } else if ($zipcode >= 3900 && $zipcode <= 4999) {
      $st = 'ME';
      $state = 'Maine';
  } else if ($zipcode >= 20600 && $zipcode <= 21999) {
      $st = 'MD';
      $state = 'Maryland';
  } else if ( ($zipcode >= 1000 && $zipcode <= 2799) || ($zipcode == 5501) || ($zipcode == 5544 ) ) {
      $st = 'MA';
      $state = 'Massachusetts';
  } else if ($zipcode >= 48000 && $zipcode <= 49999) {
      $st = 'MI';
      $state = 'Michigan';
  } else if ($zipcode >= 55000 && $zipcode <= 56899) {
      $st = 'MN';
      $state = 'Minnesota';
  } else if ($zipcode >= 38600 && $zipcode <= 39999) {
      $st = 'MS';
      $state = 'Mississippi';
  } else if ($zipcode >= 63000 && $zipcode <= 65999) {
      $st = 'MO';
      $state = 'Missouri';
  } else if ($zipcode >= 59000 && $zipcode <= 59999) {
      $st = 'MT';
      $state = 'Montana';
  } else if ($zipcode >= 27000 && $zipcode <= 28999) {
      $st = 'NC';
      $state = 'North Carolina';
  } else if ($zipcode >= 58000 && $zipcode <= 58999) {
      $st = 'ND';
      $state = 'North Dakota';
  } else if ($zipcode >= 68000 && $zipcode <= 69999) {
      $st = 'NE';
      $state = 'Nebraska';
  } else if ($zipcode >= 88900 && $zipcode <= 89999) {
      $st = 'NV';
      $state = 'Nevada';
  } else if ($zipcode >= 3000 && $zipcode <= 3899) {
      $st = 'NH';
      $state = 'New Hampshire';
  } else if ($zipcode >= 7000 && $zipcode <= 8999) {
      $st = 'NJ';
      $state = 'New Jersey';
  } else if ($zipcode >= 87000 && $zipcode <= 88499) {
      $st = 'NM';
      $state = 'New Mexico';
  } else if ( ($zipcode >= 10000 && $zipcode <= 14999) || ($zipcode == 6390) || ($zipcode == 501) || ($zipcode == 544) ) {
      $st = 'NY';
      $state = 'New York';
  } else if ($zipcode >= 43000 && $zipcode <= 45999) {
      $st = 'OH';
      $state = 'Ohio';
  } else if (($zipcode >= 73000 && $zipcode <= 73199) || ($zipcode >= 73400 && $zipcode <= 74999) ) {
      $st = 'OK';
      $state = 'Oklahoma';
  } else if ($zipcode >= 97000 && $zipcode <= 97999) {
      $st = 'OR';
      $state = 'Oregon';
  } else if ($zipcode >= 15000 && $zipcode <= 19699) {
      $st = 'PA';
      $state = 'Pennsylvania';
  } else if ($zipcode >= 300 && $zipcode <= 999) {
      $st = 'PR';
      $state = 'Puerto Rico';
  } else if ($zipcode >= 2800 && $zipcode <= 2999) {
      $st = 'RI';
      $state = 'Rhode Island';
  } else if ($zipcode >= 29000 && $zipcode <= 29999) {
      $st = 'SC';
      $state = 'South Carolina';
  } else if ($zipcode >= 57000 && $zipcode <= 57999) {
      $st = 'SD';
      $state = 'South Dakota';
  } else if ($zipcode >= 37000 && $zipcode <= 38599) {
      $st = 'TN';
      $state = 'Tennessee';
  } else if ( ($zipcode >= 75000 && $zipcode <= 79999) || ($zipcode >= 73301 && $zipcode <= 73399) ||  ($zipcode >= 88500 && $zipcode <= 88599) ) {
      $st = 'TX';
      $state = 'Texas';
  } else if ($zipcode >= 84000 && $zipcode <= 84999) {
      $st = 'UT';
      $state = 'Utah';
  } else if ($zipcode >= 5000 && $zipcode <= 5999) {
      $st = 'VT';
      $state = 'Vermont';
  } else if ( ($zipcode >= 20100 && $zipcode <= 20199) || ($zipcode >= 22000 && $zipcode <= 24699) || ($zipcode == 20598) ) {
      $st = 'VA';
      $state = 'Virgina';
  } else if ( ($zipcode >= 20000 && $zipcode <= 20099) || ($zipcode >= 20200 && $zipcode <= 20599) || ($zipcode >= 56900 && $zipcode <= 56999) ) {
      $st = 'DC';
      $state = 'Washington DC';
  } else if ($zipcode >= 98000 && $zipcode <= 99499) {
      $st = 'WA';
      $state = 'Washington';
  } else if ($zipcode >= 24700 && $zipcode <= 26999) {
      $st = 'WV';
      $state = 'West Virginia';
  } else if ($zipcode >= 53000 && $zipcode <= 54999) {
      $st = 'WI';
      $state = 'Wisconsin';
  } else if ($zipcode >= 82000 && $zipcode <= 83199) {
      $st = 'WY';
      $state = 'Wyoming';
  } else {
      $st = 'none';
      $state = 'none';
      return 'No state found matching' . $zipcode;
  }

  return $st;
}

在使用所提供答案的未修改版本之前,请务必考虑您的具体需求。

例如,如果您需要如下所示的“扩展”位置,您可能需要调整提供的代码以包含以下区域:

这里有一些(粗略的)调整可以添加到当前接受的 Tony Brasunas 答案中:

  } else if (zipcode >= 601 && zipcode <= 795) || (zipcode >= 901 && zipcode <= 988) { // Using (zipcode >= 300 && zipcode <= 999) includes VI
      st = 'PR';
      state = 'Puerto Rico'; // https://www.zip-codes.com/state/pr.asp
 } else if (zipcode >= 801 && zipcode <= 851) {
      st = 'VI';
      state = 'Virgin Islands'; // https://www.zip-codes.com/state/vi.asp
 } else if (zipcode >= 9001 && zipcode <= 9977) {
      st = 'AE';
      state = 'Armed Forces Europe'; // https://www.zip-codes.com/state/ae.asp
 } else if (zipcode >= 34001 && zipcode <= 34095) {
      st = 'AA';
      state = 'Armed Forces Americas'; // https://www.zip-codes.com/state/aa.asp
 }