Solved

parsing php string for lat and long

Posted on 2007-03-29
3
374 Views
Last Modified: 2008-03-06
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
Comment
Question by:jimfrith
3 Comments
 
LVL 24

Accepted Solution

by:
glcummins earned 500 total points
ID: 18818373
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
 

Author Comment

by:jimfrith
ID: 18818416
thanks for that
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 18818622
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now