Editing data in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Fusioned
Forum Commoner
Posts: 32
Joined: Tue Jan 18, 2005 10:43 pm
Location: Philadelphia, PA

Editing data in PHP

Post by Fusioned »

Allright, I'm working with some Google Maps crap with Zip codes and all. There's a site called GeoCoder.Us that allows you to look up places and get the Lat/Long.

With that being said, you can use a custom URL with the zipcode to get that particular Lat/Long info:

Ex: Go to this URL...: http://geocoder.us/service/csv/geocode?zip=95472

...and it will give you this string of text:

38.393314, -122.83666, Sebastopol, CA, 95472


I want to take the returned string of text above and chop everything after the second "," off so that I just end up with: 38.393314, -122.83666

That way I can plug it into Google Maps using a variable.

Any ideas? I just need the PHP code to chop off everything that second "," comma everytime. Thanks.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

How about:

Code: Select all

$str = '38.393314, -122.83666, Sebastopol, CA, 95472';
$fields = explode(',', $str);
$position = $fields[0] . ',' . $fields[1];
(#10850)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

function getCoordinates($zip) {
   $content = file_get_contents('http://geocoder.us/service/csv/geocode?zip='.$zip);
   $coordinates = explode(',' $content);
   if (count($coodinates)) {
      return array($coordinates[0], $coordinates[1]);
   }
}

echo getCoordinates(90210);
:wink:
User avatar
Fusioned
Forum Commoner
Posts: 32
Joined: Tue Jan 18, 2005 10:43 pm
Location: Philadelphia, PA

Post by Fusioned »

Jcart wrote:

Code: Select all

function getCoordinates($zip) {
   $content = file_get_contents('http://geocoder.us/service/csv/geocode?zip='.$zip);
   $coordinates = explode(',' $content);
   if (count($coodinates)) {
      return array($coordinates[0], $coordinates[1]);
   }
}

echo getCoordinates(90210);
:wink:
Your code seems to work, but I'm getting an error trying to use it.

Parse error: parse error, unexpected T_VARIABLE in /var/www/tags/test.php on line 4

Odd. Any other workarounds?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mispelt coordinates (no 'r')
Post Reply