PHP : Get coordinates latitude/longitude from string
Problem :
Let say we are dealing with another program that produces coordinates (lat/long) in a string format that look like this :
"(142.6278389135, 43.29162915041)"
and we want to assign the float values into variables.
Solution :
Break the string with explode()
function, cast the string values into float with floatval() functions. See codes below :
<?php
$str = "(142.6278389135, 43.29162915041)";
// remove the parentheses
$clean = substr($str, 1, -1);
$coord = explode(', ', $clean);
$long = floatval($coord[0]); // cast string to float
$lat = floatval($coord[1]); // case string to float
echo "Long : ".$long."\r\n";
echo "Lat : ".$lat."\r\n";
?>
Output :
Long : 142.6278389135
Lat : 43.29162915041
Reference :
See also : PHP : Extract part of a string starting from the middle
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.8k Golang : Experimenting with the Rejang script
+5.1k Golang : Return multiple values from function
+21.8k Golang : Match strings by wildcard patterns with filepath.Match() function
+5.7k Golang : List all packages and search for certain package
+20.5k PHP : Convert(cast) int to double/float
+25.5k Golang : How to read integer value from standard input ?
+7.1k Golang : Calculate how many weeks left to go in a given year
+18k Golang : Get path name to current directory or folder
+22.5k Golang : untar or extract tar ball archive example
+9.7k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+5.6k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+38.8k Golang : How to iterate over a []string(array)