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.7k Facebook : How to force facebook to scrape latest URL link data?
+8k Golang : Get final or effective URL with Request.URL example
+9.1k Golang : How to get garbage collection data?
+19.9k Golang : Reset or rewind io.Reader or io.Writer
+17.2k Golang : Check if IP address is version 4 or 6
+9.2k Golang : How to protect your source code from client, hosting company or hacker?
+15.3k Golang : ROT47 (Caesar cipher by 47 characters) example
+8.9k Golang : How to use Gorilla webtoolkit context package properly
+10k Golang : Compare files modify date example
+16k Golang : How to check if input from os.Args is integer?
+5.7k Golang : Launching your executable inside a console under Linux
+16.5k Golang : Gzip file example