Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,466 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,424 people online right now. Registration is fast and FREE... Join Now!




Zip Code to City, State

 
Reply to this topicStart new topic

Zip Code to City, State

Nova Dragoon
post 11 Apr, 2005 - 06:02 PM
Post #1


The Innocent Shall Suffer, Big Time

Group Icon
Joined: 16 Aug, 2001
Posts: 6,128



Thanked 4 times

Dream Kudos: 515

Expert In: Python, Linux

My Contributions


Does anybody know of any packages that translate zip codes to city and state?
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 11 Apr, 2005 - 06:40 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,173



Thanked 33 times

Dream Kudos: 25
My Contributions


Not sure if it's still valid, but this used to be available...it's a free zipcode search...might want to check it to see if it's still there. This one uses the search from the USPS.
User is offlineProfile CardPM

Go to the top of the page

ebourqui
post 29 Sep, 2008 - 12:45 PM
Post #3


New D.I.C Head

*
Joined: 29 Sep, 2008
Posts: 1

Here is a place to download ZIP codes with their preferred city name and 2 character state name. Put it in a database and do lookups.

http://zipinfo.com/products/z5lte/z5lte.htm
User is offlineProfile CardPM

Go to the top of the page

michaeltherrien
post 7 Oct, 2008 - 06:36 PM
Post #4


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 1

I just modified a script I found for you. I think this is what you want:

CODE

<?
/ ********************************************************************************
*****
*
* FILENAME: zip.php
* MODIFIED AND OPTIMIZED from http://www.zend.com//tips/tips.php?ozid=249&single=1
* MODIFIED BY MICHAEL THERRIEN
* 10/07/08
* Version 1.0
* http://www.michaeltherrien.com
* This script uses the free zipcode search engine at zipinfo.com to retrieve the city and state that a zipcode is associated with.
* If the script doesn't work, make sure that 'allow_url_fopen' is set to 1 (on) in your php.ini file.
* $city and $state are the variables it outputs
*
********************************************************************************
******/

function getpgdata($zip) { //Function to retrieve the contents of a webpage and put it into $pgdata
$pgdata =""; //initialize $pgdata
$fd = fopen("http://zipinfo.com/cgi-local/zipsrch.exe?zip=$zip","r"); //open the url based on the user input and put the data into $fd
while(!feof($fd)) //while loop to keep reading data into $pgdata till its all gone
{
$pgdata .= fread($fd, 1024); //read 1024 bytes at a time
}
fclose($fd); //close the connection
return $pgdata; //return the entire page data in $pgdata
}


switch ($do) {
case "retrieve":
//this is what we do if $do is set to retrieve
$pgdata = getpgdata($zip); //Use getpgdata function to read the specified zip code information

if (preg_match("/is not currently assigned/", $pgdata)) { //display an error if the zip code is not found
echo "<html><head><title>Zip Code Not Assigned</title></head><body>";
echo 'That Zip Code is currently not assigned, please try another zip code:<br />
<form method="POST" action="zip.php?do=retrieve">
<table>
<tr><td>Enter Zip Code:</td><td><input type="text" name="zip" size="5" maxlength="5"></td></tr>
<tr><td colspan="2"><input type="submit" value="Get Zipcode Information"></td></tr>
</table>
</form>
</body>
</html>';
} else {

//if a zipcode is found, then we need to find where the city starts and ends
$citystart = strpos($pgdata, "Code</th></tr><tr><td align=center>");
$citystart = $citystart + 35;
$pgdata = substr($pgdata, $citystart);
$cityend = strpos($pgdata, "</font></td><td align=center>");
$city = substr($pgdata, 0, $cityend);

$statestart = strpos($pgdata, "</font></td><td align=center>");
$statestart = $statestart + 29;
$pgdata = substr($pgdata, $statestart);
$stateend = strpos($pgdata, "</font></td><td align=center>");
$state = substr($pgdata, 0, $stateend);

//INSERT YOUR OWN CODE HERE
//THE VARIABLES ARE $city AND $state
//THIS CAN BE ADAPTED TO ALMOST ANYTHINK YOU ARE LOOKING TO DO.
echo"ZIP: $zip
<br />City: $city
<br />State: $state";

} //end of if
exit;

default:
//what to do if $do isn't set
?>
<html>

<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<title>City & State Retrieval</title>
</head>

<body bgcolor="#ffffff">
<form method="POST" action="zip.php?do=retrieve">
<table>
<tr><td>Enter Zip Code:</td><td><input type="text" name="zip" size="5" maxlength="5"></td></tr>
<tr><td colspan="2"><input type="submit" value="Get Zipcode Information"></td></tr>
</table>
</form>
</body>

</html>
<?
} //end of switch $do
//end of zip.php
?>
User is offlineProfile CardPM

Go to the top of the page

engale
post 7 Oct, 2008 - 09:05 PM
Post #5


D.I.C Addict

Group Icon
Joined: 30 Sep, 2008
Posts: 549



Thanked 2 times

Dream Kudos: 50
My Contributions


Reworked code to return in an array with the array IDs being zip, city and state.

CODE

/ ********************************************************************************

*****
*
* FILENAME: zip.php
* MODIFIED AND OPTIMIZED from http://www.zend.com//tips/tips.php?ozid=249&single=1
* MODIFIED BY MICHAEL THERRIEN
* 10/07/08
* Version 1.0
* http://www.michaeltherrien.com
* This script uses the free zipcode search engine at zipinfo.com to retrieve the city and state that a zipcode is associated with.
* If the script doesn't work, make sure that 'allow_url_fopen' is set to 1 (on) in your php.ini file.
* $city and $state are the variables it outputs
*
********************************************************************************

******/
<?
function get_zip_info($zip) { //Function to retrieve the contents of a webpage and put it into $pgdata
  $pgdata =""; //initialize $pgdata
  $fd = fopen("http://zipinfo.com/cgi-local/zipsrch.exe?zip=$zip","r"); //open the url based on the user input and put the data into $fd
  while(!feof($fd)) {//while loop to keep reading data into $pgdata till its all gone
    $pgdata .= fread($fd, 1024); //read 1024 bytes at a time
  }
  fclose($fd); //close the connection
  if (preg_match("/is not currently assigned/", $pgdata)) {
    $city = "N/A";
        $state = "N/A";
  }
  else {
      $citystart = strpos($pgdata, "Code</th></tr><tr><td align=center>");
    $citystart = $citystart + 35;
    $pgdata = substr($pgdata, $citystart);
    $cityend = strpos($pgdata, "</font></td><td align=center>");
    $city = substr($pgdata, 0, $cityend);
  
    $statestart = strpos($pgdata, "</font></td><td align=center>");
    $statestart = $statestart + 29;
    $pgdata = substr($pgdata, $statestart);
    $stateend = strpos($pgdata, "</font></td><td align=center>");
    $state = substr($pgdata, 0, $stateend);
    }
     $zipinfo[zip] = $zip;
     $zipinfo[city] = $city;
     $zipinfo[state] = $state;
     return $zipinfo;
}
if ( $_POST['zip'] == "" ) {
  ?>
    <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="text" name="zip" maxlength="5" size="4">
    <input type="submit" value="Find Zip Info">
    </form>
  <?
}
else {
  $works = get_zip_info($_POST['zip']);
  echo "Zip Code: ".$works[zip]."<br>City: ".$works[city]."<br>State: ".$works[state];
}
?>


Personaly I think it works better that way.

This post has been edited by engale: 7 Oct, 2008 - 09:05 PM
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 7 Oct, 2008 - 09:06 PM
Post #6


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,328



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


http://akroncdnr.com/test.html
User is offlineProfile CardPM

Go to the top of the page

Jayman
post 8 Oct, 2008 - 06:42 AM
Post #7


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,839



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


To bad this topic is over 3 1/2 years old.
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 8 Oct, 2008 - 07:57 AM
Post #8


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


lulz
User is offlineProfile CardPM

Go to the top of the page

engale
post 8 Oct, 2008 - 08:50 AM
Post #9


D.I.C Addict

Group Icon
Joined: 30 Sep, 2008
Posts: 549



Thanked 2 times

Dream Kudos: 50
My Contributions


Well still, I'm sure it'll help people comeing by to look up zip code info.
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 8 Oct, 2008 - 08:56 AM
Post #10


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


I'll go ahead and take the active thread and let everyone know that if they are looking to do radial searching, there is a free PHP/MySQL package that will allow you to do so. It can be found at:

PHP Zip Code Range and Distance Calculation

Hope that helps some people
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 01:38PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month