[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

parsing php string for lat and long

Im using an http request to get data that looks like this.

$xml = map.centerAndZoom(new GPoint(-82.81649, 42.175552), 4);

how can I parse this string to get the lat and long only.

$lat =
$long =
0
jimfrith
Asked:
jimfrith
1 Solution
 
glcumminsCommented:
This is not a fancy regex, but you can get what you need using strpos and strrpos:

<?php

$xml = "map.centerAndZoom(new GPoint(-82.81649, 42.175552), 4)";

$start = 1+strrpos($xml, '(');
$end = strpos($xml, ')');

list($latitude, $longitude) = explode(', ', substr($xml, $start, ($end-$start)));

echo "Latitude: $latitude<br />";
echo "Longitude: $longitude<br />";

?>
0
 
jimfrithAuthor Commented:
thanks for that
0
 
Steve BinkCommented:
In case the string position changes, you can try this:

<?
$str = '$xml = map.centerAndZoom(new GPoint(-82.81649, 42.175552), 4);';
$m = "/GPoint\(([\-0-9.]+), ([\-0-9.]+)\)/";
$a=array();
preg_match($m, $str, $a);
print_r($a);
?>
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now