webdevRefinery Forum: Splitting a string of coordinates into an x array and y array. - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Rate Topic: -----

User is offline twally333 

  • Group: Members
  • Posts: 33
  • Joined: 19-June 11

Posted 08 December 2011 - 08:36 PM (#1)

Splitting a string of coordinates into an x array and y array.


Hey all.

I'm working on a site where a little bit of geolocation is involved and what I'm trying to do is convert a string of coordinates:

(-35.01200165057613, 138.48046898164057)(-34.84423947483729, 138.46398948945307)(-34.813802923579594, 138.72491478242182)(-35.006377611621886, 138.71530174531244)


into two arrays of lat and long ie:
$lat = array(-35.01200165057613, -34.84423947483729, -34.813802923579594, -35.006377611621886);
$lng = array( 138.48046898164057, 138.46398948945307, 138.72491478242182, 138.71530174531244);


I've been looking through the php.net manuals for a good hour or so and I am stuck on how to do it. If anyone can point me to any tutorials which might be useful that would be great.

Thanks,
Tom
0


User is offline Daniel15 

  • dan.cx
  • Group: Moderators
  • Posts: 3415
  • Joined: 17-April 10
  • LocationMelbourne, Australia
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 08 December 2011 - 09:24 PM (#2)

Here's some code to start with:
<pre>
<?php
$data = '(-35.01200165057613, 138.48046898164057)(-34.84423947483729, 138.46398948945307)(-34.813802923579594, 138.72491478242182)(-35.006377611621886, 138.71530174531244)';

// Matches any number, including dots (.) and hyphens (-) and puts it into a capture group
$number = '([0-9\.\-]+)';
// Matches (number, number)
$regex = '~\(' . $number . ', ' . $number . '\)~';

// Get all (number, number) pairs from the data
preg_match_all($regex, $data, $matches);

// First group is [1], second is [2]
echo 'Lat: ';
print_r($matches[1]);
echo 'Lon: ';
print_r($matches[2]);

?>

Daniel15! :D
Posted Image

Repeat after me: jQuery is not JavaScript. It is not the answer to every JavaScript-related question. When you have to write some JavaScript, do not instantly react with "Oh, I'll do that with jQuery!"

Spoiler
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 08 December 2011 - 09:26 PM (#3)

Okay honestly it should be a Point object, but anyway use explode and then loop through and move them.

I dislike you Daniel15, I was working on one too.
Spoiler

Reserved.
0


User is offline twally333 

  • Group: Members
  • Posts: 33
  • Joined: 19-June 11

Posted 09 December 2011 - 10:14 AM (#4)

hah, Great! works a treat. Thanks for the help, as always!
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services