9 Replies - 19634 Views - Last Post: 08 October 2008 - 09:56 AM Rate Topic: -----

#1 Nova Dragoon  Icon User is offline

  • The Innocent Shall Suffer, Big Time
  • member icon

Reputation: 36
  • View blog
  • Posts: 6,169
  • Joined: 16-August 01

Zip Code to City, State

Posted 11 April 2005 - 07:02 PM

Does anybody know of any packages that translate zip codes to city and state?
Is This A Good Question/Topic? 0
  • +

Replies To: Zip Code to City, State

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Zip Code to City, State

Posted 11 April 2005 - 07:40 PM

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.
Was This Post Helpful? 0
  • +
  • -

#3 ebourqui  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-September 08

Re: Zip Code to City, State

Posted 29 September 2008 - 01:45 PM

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/p...z5lte/z5lte.htm
Was This Post Helpful? 0
  • +
  • -

#4 michaeltherrien  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-October 08

Re: Zip Code to City, State

Posted 07 October 2008 - 07:36 PM

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

<?
/*************************************************************************************
*
* 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
?>


Was This Post Helpful? 0
  • +
  • -

#5 engale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 3
  • View blog
  • Posts: 594
  • Joined: 30-September 08

Re: Zip Code to City, State

Posted 07 October 2008 - 10:05 PM

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

/ ********************************************************************************
*****
*
* 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: 07 October 2008 - 10:05 PM

Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5230
  • View blog
  • Posts: 27,015
  • Joined: 10-May 07

Re: Zip Code to City, State

Posted 07 October 2008 - 10:06 PM

http://akroncdnr.com/test.html
Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Zip Code to City, State

Posted 08 October 2008 - 07:42 AM

To bad this topic is over 3 1/2 years old.
Was This Post Helpful? 0
  • +
  • -

#8 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: Zip Code to City, State

Posted 08 October 2008 - 08:57 AM

lulz
Was This Post Helpful? 0
  • +
  • -

#9 engale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 3
  • View blog
  • Posts: 594
  • Joined: 30-September 08

Re: Zip Code to City, State

Posted 08 October 2008 - 09:50 AM

Well still, I'm sure it'll help people comeing by to look up zip code info.
Was This Post Helpful? 0
  • +
  • -

#10 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: Zip Code to City, State

Posted 08 October 2008 - 09:56 AM

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
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1