Jump to content

[SOLVED] Beginner Help - str_replace for multiple variables


alconebay

Recommended Posts

I have about 100 variables that are holding currency information that needs to be summed up. The users always format the currency with commas but the commas are treated like a decimal (messing up the sum) so I need to run str_replace on all the variables to remove the commas.

 

Code example:

 

$variable1
$variable2
$variable3

$variable1 = str_replace(",", "", $variable1);
$variable2 = str_replace(",", "", $variable2);
$variable3 = str_replace(",", "", $variable3);

 

My question: Do I have to make a str_replace line for each of the 100 variables or is there a way I can tell it to run str_replace on the following variables (then have all the variables listed as comma separated values or something like that)?

 

 

 

Link to comment
Share on other sites

Would be a tonne easier if you had used arrays to store the information. I'm sorry but I don't know too much about Variable Variables and if they could help you in the state you are in. Otherwise, I think you MAY need to use str_replace() on them all. But really, it could've easily been avoided if you had structured your code better, using arrays. Sorry.

Link to comment
Share on other sites

I agree an array would be better, but just for fun:

<?php
$variable1="123,456";
$variable2="789,011";
$variable3="719,022";

for ($i = 1; $i <= 100; $i++) {

    ${'variable' . $i} = str_replace(',','', ${'variable' . $i});

}
?>

It works but no error checking, fairly useless, inefficient... but that's how I roll. :)

 

 

Link to comment
Share on other sites

If your variables are sequenced like in your example, you can use variable variables:

 

<?php
for ($i = 1; $i <= 100; $i++) {
$var_name = "variable$i";
$$var_name = str_replace(',', '', $$var_name);
}
?>

 

But still, try to fill an array with the values instead of creating a variable for each.

 

Edit: Guess philip beat me to it :)

Link to comment
Share on other sites

Well, you could also:

<?php
// These are your variable names
$names = array('foo', 'bar', 'baz');

foreach ($names as $name) {
    $$name = str_replace(',', '', $$name);
}

echo "$foo $bar $baz";
?>

I'm too lazy to make it more efficient (am thinking creating array, and calling str_replace() once would be) but anyway... the above is easy.

Link to comment
Share on other sites

@ philip

 

You could use array_map() to convert the $names array to an array with the actual values. Guess it's faster than calling str_replace each time:

 

<?php
function return_var($var_name) {
    return $$var_name;
}

// These are your variable names
$names = array('foo', 'bar', 'baz');

$vars = array_map('return_var', $names);

$vars = str_replace(',', '', $vars);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.