3

I need to extract latitude and longitude coordinates from a string using php. The string will always be formatted as such:

"(42.32783298989135, -70.99989162915041)"

However the length of each value will vary from use to use. What's the best way to extract the values? Thanks!

0

6 Answers 6

13

You may use sscanf to do this:

sscanf($string, '"(%f, %f)"', $lat, $lng);

Test:

php > sscanf('"(42.32783298989135, -70.99989162915041)"', '"(%f, %f)"', $lat, $lng);
php > var_dump($lat, $lng);
float(42.327832989891)
float(-70.99989162915)
8
  • Neat solution, but note that there will be some loss of precision involved.
    – mfonda
    Jan 24, 2011 at 19:27
  • @mfonda: Could you elaborate? Do you mean that precision could be lost if there were more digits?
    – greg0ire
    Jan 24, 2011 at 19:28
  • @greg0ire: Look at your above output for example: the original input has the latitude of "42.32783298989135", but using float leaves you with just "42.327832989891" (missing the last 3 digits, which depending on the application may be important). For more information, see us3.php.net/manual/en/language.types.float.php
    – mfonda
    Jan 24, 2011 at 19:34
  • 2
    @mfonda - 42.32783298989135, -70.99989162915041 has a latitude error of 1.111 nanometers, longitude error of 0.824 nanometers (uncertainty 1.383 nanometers); 42.327832989891, 70.99989162915 has a latitude error of 111.080 nanometers, longitude error of 82.424 nanometers (uncertainty 138.320 nanometers)... yes, I do mean nanometers... that margin of rounding error should be accurate enough for most purposes.
    – Mark Baker
    Jan 24, 2011 at 19:54
  • 2
    @mfonda - while there will inevitably be a degree of error when working with floats, var_dump isn't a particularly good measure of this, as it will use PHP's precision setting when casting to string for the var_dump display; and the last three digits (in the quoted example) are lost because of the cast to string rather than the precision of the float in PHP. Set the precision to 16, and those missing digits will be displayed correctly when casting the float to string, even on a 32-bit system.
    – Mark Baker
    Jan 24, 2011 at 21:24
1
$withoutParentheses = substr($string, 1, -1);
$coordinates = explode(', ', $coordinates);
$longitude = floatval($coordinates[0]);
$latitude = floatval($coordinates[1]);
1
  • 1
    Instead of substr you can also use trim. Jan 24, 2011 at 19:23
1

You could use a regular expression to extract the numbers from the brackets and then use the explode command to split the numbers into an array.

Your regex would be something like

/(-?[0-9]+.[0-9]+, -?[0-9]+.[0-9]+)/

and your delimiter for the explode command would be a comma.

2
  • Avoid using regexp when you can.
    – greg0ire
    Jan 24, 2011 at 19:23
  • 1
    Commonly repeated mantra: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    – KevinDTimm
    Jan 24, 2011 at 19:55
0

This is what I came up with on my own:

function LatFromString($mCenterString){
    //(42.32783298989135, -70.99989162915041)
    $mLatLong = explode(",", $mCenterString);
    $mLat = '';
    if(count($mLatLong) == 2){
        $mLat = substr($mLatLong[0], 1);
    }
    return $mLat;
}
function LongFromString($mCenterString){
    //(42.32783298989135, -70.99989162915041)
    $mLatLong = explode(",", $mCenterString);
    $mLat = '';
    if(count($mLatLong) == 2){
        $mLong = substr($mLatLong[1], 0, -1);
    }
    return $mLong;
}

How does this look?

0
0

You could do something like this:

            $str = "(42.32783298989135, -70.99989162915041)";

            function extractlatlong($str){
                $latlong = explode(",",$str);

                if(count($latlong) == 2){
                    $lat = preg_replace("%[^0-9.]%","",$latlong[0]);
                    $long = preg_replace("%[^0-9.]%","",$latlong[1]);
                    return array("lat"=>$lat,"long"=>$long);
                } else {
                    return NULL;
                }
            }

            print_r(extractlatlong($str));
0

If you want to use sscanf() (and I probably would), but don't want to lose any precision from casting the numeric values as floats, then use a couple of negated character classes. You don't' have to match the trailing ) to extract what you need.

Code: (Demo)

$latlon = "(42.32783298989135, -70.99989162915041)";
sscanf($latlon, '(%[^,], %[^)]', $lat, $lon);
var_export([$lat, $lon]);

Output as an array of strings:

array (
  0 => '42.32783298989135',
  1 => '-70.99989162915041',
)

Or if you just want an indexed array of the two values like the above output:

var_export(sscanf($latlon, '(%[^,], %[^)]'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.