I want is to get the coordinates from a string (grabbed from the src of a Google Maps img). Below is a poor try of mine with regex that doesn’t work.
So, what is the best way to do this? With regex? In that case how should be composed?
$string = "//maps.google.com/maps/api/staticmap?sensor=false&center=56.393906,16.066206&zoom=12&size=344x170&language=sv&markers=56.393906,16.066206&maptype=roadmap&scale=1";
$matches = array();
preg_match('/center=(.*?)\zoom/s', $string, $matches);
... ?
Wanted end result:
$coordiates = (
[0] = '56.393906',
[1] = '16.066206'
);
You shouldn’t use a regex for something like this as it would be slower and more prone to break if URL changes. PHP has built in functions already for such a thing. Use parse_url() and parse_str() to easily accomplish it.
Outputs:
So to get your coordinates just do: