如何快速确定给定邮政编码的状态?

Posted

技术标签:

【中文标题】如何快速确定给定邮政编码的状态?【英文标题】:How can I quickly determine the State for a given zipcode? 【发布时间】:2015-05-03 12:20:08 【问题描述】:

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

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

最好是Javascript。 php 也可以工作。

【问题讨论】:

其中免费的有状态信息-unitedstateszipcodes.org/zip-code-database 您需要一个邮政编码及其各自状态的数据库。您可以从 USPS 获得一份。 这个问题的副本只有一个被赞成的答案,它是一个已删除问题的链接。 这被错误地标记为重复。 您也可以查看thezipcodes.com。 【参考方案1】:

美国邮政编码数据实际上足够稳定,如果只需要州(不是城市或其他任何东西),您无需访问 API 或数据库就可以做到这一点.

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

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;


您可以通过在最后一行返回 state 而不是 st 来返回州的全名,而不仅仅是两个字母的缩写。

非常感谢 @kevin-boucher 和 @abaldwin99 帮助他们解析较小的新英格兰代码并避免可怕的八进制评估错误,他们的答案是 here。

同时感谢this useful page提供的大部分原始代码。

【讨论】:

【参考方案2】:

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 中。

【讨论】:

很好的答案,尽管我不希望将任何外部依赖项添加到应用程序中。这很精简,但仍然取决于 zippopotam.us。【参考方案3】:
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;


【讨论】:

【参考方案4】:

您可以使用范围数组返回给定邮政编码最可能的状态。

这不是一个验证器——并不是一个范围内的每个数字都被实际分配为邮政编码,将来可能会添加新的范围。

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')

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

【讨论】:

【参考方案5】:

我意识到 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",
        _ => ""
    ;

【讨论】:

【参考方案6】:

这是@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;

【讨论】:

【参考方案7】:

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

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

VI(维尔京群岛https://www.zip-codes.com/state/vi.asp - 在某些答案中被列为 PR) AE(欧洲武装部队https://www.zip-codes.com/state/ae.asp) AA(美洲武装部队https://www.zip-codes.com/state/aa.asp)

以下是一些(粗略的)调整,可以添加到当前接受的 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
 

【讨论】:

以上是关于如何快速确定给定邮政编码的状态?的主要内容,如果未能解决你的问题,请参考以下文章

如何在sql server中通过给定的邮政编码和以英里为单位的半径从附近的表中获取所有其他邮政编码或(纬度和经度)?

如何在 iOS SDK 上通过给定的邮政编码 (ZIP) 获取国家/地区名称

验证 iPhone 上现有的邮政编码的最快方法是啥?

是否有任何服务返回给定城市/州的邮政编码?

如何验证国家和州/省名称和邮政编码?

通过其他连接获得laravel 5中的纬度和经度