You can split the string into Lat-Long pairs by splitting them around the comma delimiter.
$str="166.282008076887 -50.4981757000558,166.282014047837 -50.4981149924515,166.282009048641 -50.4981449926728,166.282021047737 -50.498071992073,166.281791047443 -50.4979599921101,166.281661047662 -50.4978739926141,166.281637048376 -50.4978479925945";
$latLongArray=explode(",",$str);
echo "<pre>";
print_r($latLongArray);
echo "</pre>";
The above splits the string into space separated Lat/Long pairs as follows -
Array
(
[0] => 166.282008076887 -50.4981757000558
[1] => 166.282014047837 -50.4981149924515
[2] => 166.282009048641 -50.4981449926728
[3] => 166.282021047737 -50.498071992073
[4] => 166.281791047443 -50.4979599921101
[5] => 166.281661047662 -50.4978739926141
[6] => 166.281637048376 -50.4978479925945
)
Next, you split them along the space, and save each lat/long array pair in a new array. You can use an associative array or json, the choice is yours.
$finalArray=array();
for($i=0;$i<count($latLongArray);$i++){
$tempStr=str_replace(" ",",",$latLongArray[$i]);
$tempArr=explode(",",$tempStr);
array_push($finalArray,$tempArr);
}
echo "<pre>";
print_r($finalArray);
echo "</pre>";
This prints the Lat/Long pairs saved in the final array -
Array
(
[0] => Array
(
[0] => 166.282008076887
[1] => -50.4981757000558
)
[1] => Array
(
[0] => 166.282014047837
[1] => -50.4981149924515
)
[2] => Array
(
[0] => 166.282009048641
[1] => -50.4981449926728
)
[3] => Array
(
[0] => 166.282021047737
[1] => -50.498071992073
)
[4] => Array
(
[0] => 166.281791047443
[1] => -50.4979599921101
)
[5] => Array
(
[0] => 166.281661047662
[1] => -50.4978739926141
)
[6] => Array
(
[0] => 166.281637048376
[1] => -50.4978479925945
)
)
Hope this helps.
Added later --
$totalDistance=0;
for($i=0;$i<count($finalArray);$i++){
$totalDistance+= calculateDistance($finalArray[$i],$finalArray[$i+1]);
}
echo $totalDistance;
function calculateDistance($pointA,$pointB){
// calculate distance between the two points
// ie, $pointA->Lat/Long, $pointB->Lat/Long
}