Page 1 of 1

Editing data in PHP

Posted: Thu Feb 16, 2006 3:48 pm
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.

Posted: Thu Feb 16, 2006 4:19 pm
by Christopher
How about:

Code: Select all

$str = '38.393314, -122.83666, Sebastopol, CA, 95472';
$fields = explode(',', $str);
$position = $fields[0] . ',' . $fields[1];

Posted: Thu Feb 16, 2006 4:20 pm
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:

Posted: Thu Feb 16, 2006 4:45 pm
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?

Posted: Fri Feb 17, 2006 12:26 am
by feyd
mispelt coordinates (no 'r')