views:

130

answers:

5

Hello, I have a question about str_replace in PHP. When I do:

$latdir = $latrichting.$Lat;

If (preg_match("/N /", $latdir)) {
    $Latcoorl = str_replace(" N ", "+",$latdir);
}
else {
    $Latcoorl = str_replace ("S ", "-",$latdir);
}

print_r($latdir);
print_r($Latcoorl);

print_r($latdir); gives :N52.2702777778

but print_r ($Latcoorl); gives :N52.270277777800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Yes, it adds a lot of zeros. Can someone explane this behavior just for the fun of it?

print_r ($latrichting);

give's: N

print_r ($Lat);

This give's the weird long number.

So its probably not the str_replace command, you think ?

+1  A: 
$latmin2 = bcdiv($latsec, 60, 20);
$latmin_total = $latmin + $latmin2;
$lat = bcdiv($latmin_total, 60, 20);

$latdir = array("N" => 1, "S" => -1);

$latcoorl = $latdir * $latdir[$latrichting];

Happy New Year.

Anthony
thank for the quick response, WOW you readers work really fast.But i have additional info because of Ivan Nevostruev.
Plumbum7
Whoever voted me down, when I'm the only person who hasn't blown the asker off and has actually thought of some creative reason for the situation rather than hiding behind the flag of "need more info. blah blah blah" should consider themselves a coward on all three counts.
Anthony
Where are you getting `$Lat` from?
Anthony
Anthony, I would thank you the effort ( i say +2 vote's) but the problem is i can't give it to you , i need 15 rep and have only 5.Youre answer is surtenly worth the effort if anything else wont work (see the last minute additions)
Plumbum7
Thanks. That means more than some snarky android's downvotes anyday. See my last minute total change for what I think you're wanting.
Anthony
Ha Ha! that's awesome that this all based on Google APIs. I've been struggling with Google Calendar API for 4 days now.
Anthony
@Plumbum7, where are the geo-coords coming from that you are trying to convert? straight user input or some other source? I be I could help you find a smoother way to convert, as Google's documentation for PHP is pretty flimsy and I've found mistakes in it before.
Anthony
Thank you very much this is probably the solution, and for the Google Calendar API good luck and i will help you if i could but i don't think i am any positive value for that (yet) HAHAHA.
Plumbum7
The are coming from a plugin in Lightroom from jeffrey Friedl.
Plumbum7
+1 for simplifying the N/S to +/- process. Much simpler to do it on $latrichting before (or, in this case, during) any concatenation. One question... is "+" necessary at all? – hlpiii 0 secs ago
hlpiii
Now that you mention, it...huh. Probably not. I'll update my code yet again. Thanks for the upvote!
Anthony
I was going to make it simply `$latcoorl = ($latrichting == "S") ? $latcoorl * -1 ? $latcoorl` but I hate when there are only two choices and the ternary sort of assumes one or else must the the other, so I made the array. Might be overkill.
Anthony
Yeah is just a matter of coding style. You've got the OP on the way to getting correct and expected output, which is the point. Style/Elegance points are optional :P
hlpiii
Style, maybe. Elegance? Never! Bare in mind I started (this time around) in CSS, so I long for the elegance in code it promises (but doesn't QUITE deliver).
Anthony
+1  A: 

Your string replace search string has a space before the 'N' while the dumped value looks like it's N:

Not sure what it has to do with all the zeros though.

Simon
This should be a comment.
Anthony
@Anthony, that is a comment ;)Point taken though.
Simon
+1 to compensate for the karma-hit of the down-vote. While I agree with Anthony, I don't find your answer (or 'comment') to be harmful to the state of understanding that the down-vote should -imho- imply.
David Thomas
I'm not hating on Simon or trying to crack a whip, I just think if I give a crazy answer that is sincere and get's voted down, then answers that don't offer (no offense) any insight into the question (even if it's right) should get pointed out. if this was made a comment, I'd vote it up as I was thinking the same thing. I'd rather be the only answer and it be crazy and voted down then drowned about well-intentioned, mis-placed comments.
Anthony
+1 Nice observation, in fact from the example it looks like that preg_match() is always evaluating to false. I think the preg_match() and both str_replace() calls are not doing what they are meant to do because of extra spaces. Anthony's solution cleans this process up a bit, so OP is on the right track now.
hlpiii
@Anthony The reason I added it as a possible answer rather than comment was that at the time I posted we didn't have any more information about where $latrichting and $Lat where coming from, so the error in the str_replace could have been effecting the outcome. I was just honest enough to say I didn't know how it would be affecting the result, again due the lack of context.
Simon
@Simon, I understand. My original answer was the same thing. I threw out what I understood, then threw out a crazy hunch (which you did not, I know), then was very honest that it was a crazy hunch but without more details it was the best I could think of. And I got downvoted. The main difference between my answers (often, not always) and others is that I assume some level of debugging before they form so specific a question. Most people demand a bigger picture whereas I (obviously wrong in this case) fill in back story.
Anthony
A: 

On my system this code fragment:

<?php
$latdir = ':N52.2702777778';

If (preg_match("/N /", $latdir)) {
    $Latcoorl = str_replace(" N ", "+",$latdir);
    }
    else {
        $Latcoorl = str_replace ("S ", "-",$latdir);
        }

print_r($latdir);
print_r($Latcoorl);
?>

gives the following result:

:N52.2702777778:N52.2702777778

My best guess is you have something after this code that prints out a serie of 0.

ntd
Yours never deals with a float, and strings won't suffer from extra zeroes. I believe the colon at the beginning is not really there, just a sort of typo in the OP. "gives: something" vs. "gives :something"
hlpiii
Taking as true the original assumption that `print_r($latdir);` gives `:N52.2702777778` I don't see any float issue: the posted code works only on strings, independently on how $latdir is got. Also without the colon, the initial N character prevents the coercion to float.If my original assumption is false... well, we are developers, not psychics ;)
ntd
A: 

How I would do it; just a variation of Anthony's original answer that keeps everything as numeric and doesn't lapse into string mode.

$Latcoorl = ($latrichting == "N") ? ($Lat) : (-1 * $Lat);
hlpiii
A: 

The string operations you did won't generate any 0s. The 0s have to come from $lat. What did you do with $lat? any division by pi? PHP will try to store the most accurate possible float number in $lat. That's not really a problem, its a correct behavior. Just truncate the number when displayed, or round it up.

Darkerstar