Forum Moderators: coopster

Message Too Old, No Replies

Using str_replace in foreach loop

         

cookie2

9:39 pm on Feb 26, 2004 (gmt 0)

10+ Year Member



I'm trying to set up a php script to handle input from a form. One thing I want to do is to replace any semicolons that were entered with commas. I have str_replace working to do it like so:

$question1 = str_replace(";", ",", $question1);
$question2 = str_replace(";", ",", $question2);

And it works. But since I have a lot of fields, I thought it would be more efficient to parse them with a foreach loop. But that's where I'm having a problem. I tried this but it doesn't work:

foreach ($_POST as $k=>$v) {
$_POST[$k] = str_replace(';', ',', $v);
$_POST[$k] = $v;
}

Can anyone show me what I need to do to get this foreach loop working properly? THanks for any help.

coopster

10:22 pm on Feb 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You are assigning the value correctly, but then you are overwriting it with the original value again. Get rid of your last line so your loop looks like this and you should be golden:
foreach ($_POST as $k=>$v) { 
$_POST[$k] = str_replace(';', ',', $v);
}



Just for clarification:

str_replace returns a string with the replacements you specified, it doesn't actually modify the original variable. This alone...

str_replace(';', ',', $my_variable);

...does not modify the original variable. If you wanted to do that, you would assign the results of the function back to the variable itself, by replacing the value...
$my_variable = str_replace(';', ',', $my_variable);

...or by assigning it to an array in the manner you used that we cleaned up. Note that you currently cannot change the values of the array directly in a foreach loop on the value parameter as stated in the manual:

Note that it is currently not possible to change the values of the array directly in such a loop. A workaround is the following [php.net].

which means:
foreach ($_POST as $k=>$v) { 
// won't work:
//$v = str_replace(';', ',', $v);
// works:
$_POST[$k] = str_replace(';', ',', $v);
}

So, good for you! You got it right on the first try!

<edit>Elaborated</edit>

[edited by: coopster at 1:12 am (utc) on Feb. 27, 2004]

cookie2

11:39 pm on Feb 26, 2004 (gmt 0)

10+ Year Member



Hmmm, I thought the $_POST[$k] = $v; in the foreach loop was intended to assign the results of the function back to the variable itself. I blew that one.

Is there an easier way to assign the results back to the variables after the str_replace has run instead of doing them one variable at a time with your $my_variable example? It seems kind of pointless to do the foreach loop if I have to go through each variable individually to assign the results back to the variable.

coopster

1:10 am on Feb 27, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No, no, you've got it right. I was trying to explain to you how variable updating works (and doesn't) work within a foreach loop. You got it right on the first attempt, except that you didn't realize you were replacing the string value and updating the variable in the $_POST array at the same time. Your loop was all good up to that point! You only needed to get rid of the last line because after you updated the $_POST array value, you were resetting it back to it's original value.

I tried to elaborate in the post so as to help you understand how it works and eliminate any confusion. Seems I did just the opposite!

mipapage

1:24 am on Feb 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



*ahem* rookie here - would array_walk work for this?

I think so, and if so, would it not be the 'better' choice in that you would avoid using a loop in your code...?