Taylor Foster
Taylor Foster

Reputation: 1215

str_replace inside a foreach within function

I am trying to update a block of code with value's sent through a form however I am having trouble updating the whole block of code. The number of inputs change based on the code. For example:

.btn-primary{
  color: @color;
  font-style: none;
  background-color: @background-color;
  transition: all .75s;
  text-transform: uppercase;
  font-weight: @font-weight;
   &:hover{
    background-color: darken(@background-color, 10%);
    font-style: none;
   color: @color; 
  }
}

I am able to sucessfully find the @ variables and display the correct number of <input>'s based off the code. The problem I am having is having my foreach loop run through each $_POST value and updating the code. The best result I have been able to get is the first $_POST value but then it cuts off.

function replace_code($code){
 foreach($_POST as $name => $value){
  return str_replace($name, htmlentities($value), htmlentities($code));
 }
}

In the case of .btn-primary when I fill out my inputs and submit the form the best I can get is the first value to update and that is it. If I were to put #fff in the @color field and other values into @background-color and @font-weight and run my function once submit is set, I get the output of.

.btn-primary{
 color: #fff;
 font-style: none;
 background-color: @background-color;
 transition: all .75s;
 text-transform: uppercase;
 font-weight: @font-weight;
  &:hover{
   background-color: darken(@background-color, 10%);
   font-style: none;
   color: #fff; 
  }
 }

Any idea's on how I can get that foreach loop to keep updating the code.

Upvotes: 0

Views: 122

Answers (4)

Barmar
Barmar

Reputation: 781761

If you want to do multiple replacements, you can give str_replace arrays of the old and new strings, you don't need to use a loop.

function replace_code($code) {
    $names = array_keys($_POST);
    $values = array_map('htmlentities', array_values($_POST));
    return str_replace($names, $values, $code);
}

Upvotes: 0

JoniJnm
JoniJnm

Reputation: 722

Your PHP function replace only the first value, you have to loop it:

function replace_code($code){
    //best function
    $from = array();
    $to = array();
    foreach($_POST as $name => $value) {
        $from[] = $name;
        $to[] = htmlentities($value);
    }
    return str_replace($from, $to, $code);
}

function replace_code($code){
    foreach($_POST as $name => $value) {
        $code = str_replace($name, htmlentities($value), $code);
    }
    return $code;
}

Use htmlentities if you need it

Upvotes: 0

Machavity
Machavity

Reputation: 31654

You're issuing a return which is terminating your function. Either you need to call each $_POST separately to that function (inefficient) or you need to parse the data into a data structure

function replace_code($code){
 $data = array();
 foreach($_POST as $name => $value){
  $data[$name] = str_replace($name, htmlentities($value), htmlentities($code));
 }
 return $data;

}

Upvotes: 1

Shujaat
Shujaat

Reputation: 691

The return should be after the foreach loop, since it's inside the loop it will exit the function on first loop

function replace_code($code){
 foreach($_POST as $name => $value){
  $code = str_replace($name, htmlentities($value), $code);
 }

return htmlentities($code);
}

Upvotes: 2

Related Questions