jakebur01 Posted July 30, 2014 Share Posted July 30, 2014 I have a string I am pulling out of a kml file to be used in google's static map image. The problem is that the longitude and latitude are in the wrong order. $xml=simplexml_load_file($kml); //print_r($xml); $coordinates= $xml->Document->Placemark->Polygon->outerBoundaryIs->LinearRing->coordinates; $coordinates = str_replace(",0","|",$coordinates); echo $coordinates; $coordinates returns -93.93740385636626,32.5298698657008| -93.94147866742821,32.52989552832681| -93.94144945527509,32.53330102162547| -93.94975589137293,32.53335196474311| -93.94983123242544,32.52989370087431| -93.94493319136991,32.52989645540433| -93.9443121915328,32.52861950574362| -93.94419758821717,32.52736388060429| -93.94302907878635,32.52652644904565| -93.94229993303671,32.5257441308524| -93.94147580320451,32.52521094897828| -93.94137978713827,32.52261795379491| -93.93742258613195,32.5225916055261| -93.93740385636626,32.5298698657008| I need them as they are above separated by pipes to be used as a parameter in a url only I need the longitude and latitude swapped. So instead of "-93.93740385636626,32.5298698657008", I need it to read "32.5298698657008,-93.93740385636626". All of the coordinates I will be using in the future will be in the same general area as the locations above. Quote Link to comment https://forums.phpfreaks.com/topic/290187-swap-longitude-and-latitude-in-a-string/ Share on other sites More sharing options...
Solution cyberRobot Posted July 30, 2014 Solution Share Posted July 30, 2014 Here's a quick script for swapping the values: <?php //INITIALIZE VARIABLES $values = "-93.93740385636626,32.5298698657008| -93.94147866742821,32.52989552832681| -93.94144945527509,32.53330102162547| -93.94975589137293,32.53335196474311| -93.94983123242544,32.52989370087431| -93.94493319136991,32.52989645540433| -93.9443121915328,32.52861950574362| -93.94419758821717,32.52736388060429| -93.94302907878635,32.52652644904565| -93.94229993303671,32.5257441308524| -93.94147580320451,32.52521094897828| -93.94137978713827,32.52261795379491| -93.93742258613195,32.5225916055261| -93.93740385636626,32.5298698657008|"; $coords = explode('|', $values); $correctedCoords = array(); //LOOP THROUGH THE COORDINATES foreach($coords as $currLatLong) { //IF THE CURRENT ENTRY CONTAINS A VALUE, SWAP THE VALUES $currLatLong = trim($currLatLong); if($currLatLong != '') { list($val1, $val2) = explode(',', $currLatLong); $correctedCoords[] = "$val2,$val1"; } } //RE-BUILD THE STRING $correctedCoords = implode('| ', $correctedCoords); //OUTPUT RESULTS print '<pre>' . print_r($values, true) . '</pre>'; print '<pre>' . print_r($correctedCoords, true) . '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/290187-swap-longitude-and-latitude-in-a-string/#findComment-1486477 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.