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
+3.2k Android Studio : Indicate progression with ProgressBar example
+5.6k Golang : Get current time from the Internet time server(ntp) example
+17.8k Golang : Upload to S3 with official aws-sdk-go package
+6.9k Golang : How to reverse elements order in map ?
+21.4k Golang : Download file example
+12.7k Golang : How to count the number of repeated characters in a string?
+2.5k Swift : Convert string array to array example
+2.1k JavaScript : Rounding number to decimal formats to display currency
+8.5k Facebook PHP getUser() returns 0
+14k Golang : Calculate time different
+5.4k Javascript : Read/parse JSON data from HTTP response
+4.5k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example