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
+19.8k Golang : Compare floating-point numbers
+27.7k Golang : Move file to another directory
+35.8k Golang : How to split or chunking a file to smaller pieces?
+26.5k Golang : Convert file content into array of bytes
+7k Golang : Of hash table and hash map
+9k Golang : How to control fmt or log print format?
+12.8k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+11.4k Golang : Secure file deletion with wipe example
+46k Golang : Encode image to base64 example
+14k Golang : How to convert a number to words
+5.2k Golang : What is StructTag and how to get StructTag's value?
+15k Golang : Find location by IP address and display with Google Map