ERuiz Posted December 25, 2006 Share Posted December 25, 2006 Hello all,If I have a variable that stores a coordinate exactly in this format:N18 23.5725 W66 23.8565And I need to change this coordinate into decimal degrees such as this:18.3928, -66.3976In the example above, in order to get the .xxxx value, we need to divide xx.xxxx by 60 (ej: 23.5725/60 = .3928). Also, I need to have a comma separating the 2 values.How can this be accomplished? Keep in mind that the letter W or S, needs to be converted to a - (negative sign)The name of the variable is $positionThanks for any help.Regards,Efrain Ruiz Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 25, 2006 Share Posted December 25, 2006 which way do you need to convert? if is N18 23.5725 is better to have to variables, one for N18 and the other for 23.5725. Quote Link to comment Share on other sites More sharing options...
ERuiz Posted December 25, 2006 Author Share Posted December 25, 2006 What you mean, my friend?Essentially, I just want the final variable to be in the following format: "xx.xxxx, xx.xxxx" instead of being "Nxx xx.xxxx Wxx xx.xxxx"Maybe you can separate the values for better manipulation, but once all converting is done, I would like to have the final variable as described above. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 25, 2006 Share Posted December 25, 2006 <?php$degrees = explode(" ",$position); $number = $degrees[1]/60;if (substr($degrees[0], 0, 1) == N){$answer = $number + substr($degrees[0], 1, 4);}if (substr($degrees[0], 0, 1) == W){$answer = 0 - 2*($number + substr($degrees[0], 1, 4);}?>sorry, the 1 should be 0 Quote Link to comment Share on other sites More sharing options...
ERuiz Posted December 25, 2006 Author Share Posted December 25, 2006 Do I need to change the {} to ()? Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 25, 2006 Share Posted December 25, 2006 nope, those cannot be brackets... Quote Link to comment Share on other sites More sharing options...
ERuiz Posted December 25, 2006 Author Share Posted December 25, 2006 Ok, I will give it a try :-) Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 25, 2006 Share Posted December 25, 2006 hey, Efrain, does it work, i modified it, the $degrees[0] in the if statements should be $degrees[1], you see them? Quote Link to comment Share on other sites More sharing options...
ERuiz Posted December 25, 2006 Author Share Posted December 25, 2006 Hi buddy,I gave this a try and I get the following error:syntax error, unexpected ';'The line it says the error is in, points to the second "if" line Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 25, 2006 Share Posted December 25, 2006 opps sorry, i forgot to close the if bracket...<?php$degrees = explode(" ",$position); $number = $degrees[1]/60;if (substr($degrees[0], 0, 1) == N){$answer = $number + substr($degrees[0], 1, 4);}if (substr($degrees[0], 0, 1) == W){$answer = 0 - 2*($number + substr($degrees[0], 1, 4));}?> Quote Link to comment Share on other sites More sharing options...
ERuiz Posted December 25, 2006 Author Share Posted December 25, 2006 Cool, now I didn't get the error. But how do I display the final value? What is the variable the result is stored in? Pardon my ignorance... :-[ Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 25, 2006 Share Posted December 25, 2006 the value is stored in $answer... if you tried echo $answer; and nothing comes out, then there is a syntax problem Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 25, 2006 Share Posted December 25, 2006 I think there are some errors with the code, hold on, i will have a look at it Quote Link to comment Share on other sites More sharing options...
ERuiz Posted December 25, 2006 Author Share Posted December 25, 2006 I tried the echo command, and nothing was displayed. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 25, 2006 Share Posted December 25, 2006 here this should work...<?php$degrees = explode(" ",$position); $number = ($degrees[1]/60);if (substr($degrees[0], 0, 1) == "N"){$answer = $number + substr($degrees[0], 1, 3);}if (substr($degrees[0], 0, 1) == "W"){$answer = 0 - ($number + substr($degrees[0], 1, 3));}echo "$answer";?>MODIFIED, PLEASE RECHECK AGAIN... Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 25, 2006 Share Posted December 25, 2006 okay, yeah it works, i tested for N18 23.5725 :P took me a while... by the way, how many digits are there for N18 or W66? because my script only works if there are two digits (18 and 66) Quote Link to comment Share on other sites More sharing options...
ERuiz Posted December 25, 2006 Author Share Posted December 25, 2006 Ok, it works but partially.In the current scenario, $Position has this value "N40 38.4541 W73 47.0027"$answer is giving this result "40.6409016667"As you can see, it is doing the correct conversion but it's missing the other coordinate. So far so good my friend! ;-) You are almost there! Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 25, 2006 Share Posted December 25, 2006 This is the last post I am making...[code]<?php$position = "W66 23.8565";//here we give a dummy variable to see if it works or not, and all your other inputs should be in this format...$degrees = explode(" ",$position); $number = ($degrees[1]/60);if (substr($degrees[0], 0, 1) == "N"){$answer = $number + substr($degrees[0], 1, 3);}//if is N, we leave the numberif (substr($degrees[0], 0, 1) == "W"){$answer = 0 - ($number + substr($degrees[0], 1, 3));}//if is W, we do 0 minus the answer so we get the negative answer..echo $answer;//here we echo the answer?>[/code]hope you got through it... and i am really bad coder, kind of speeding my progress as well, so a bit rushed...this should work no matter two or three digits of N or W...hope you become a great mathmatician ERuiz ;) Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 25, 2006 Share Posted December 25, 2006 sorry, i didnt see your last post...i cant separate "N40 38.4541 W73 47.0027", because there are also spaces bewteen the N40 and 38 as well as W73 and 47, if you have them like this: "N40 38.4541-W73 47.0027", it will be much easier as well as this: "N40-38.4541 W73-47.0027". Quote Link to comment Share on other sites More sharing options...
ERuiz Posted December 25, 2006 Author Share Posted December 25, 2006 hmmmm So if there are 2 whitespaces, we can't separate them or trim the values? Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 25, 2006 Share Posted December 25, 2006 not exactly, an expert might find a way out, but i am kind of a starter... Quote Link to comment Share on other sites More sharing options...
ERuiz Posted December 25, 2006 Author Share Posted December 25, 2006 Starter? I wish I knew what you know! lol But seriously, I am in debt for all your help, my friend. I will use what you gave me to experiment around. If I come up with something, I will let you know! I think Thanks a million! Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 25, 2006 Share Posted December 25, 2006 hold on.. let me think logically... the computer goes from left to right... so it recognizes the spaces in order right? hold on a while... Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 25, 2006 Share Posted December 25, 2006 can you give me the answer to the second one please, i want to check... Quote Link to comment Share on other sites More sharing options...
ERuiz Posted December 25, 2006 Author Share Posted December 25, 2006 Yup, it checks from left to right. Checkout this page, regarding the explode() functionhttp://www.phpdig.net/ref/rn58re1163.htmlhttp://us3.php.net/explode Quote Link to comment 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.