title: changing a string 'recursively' in PHP
body: <p>I want to change a string (in PHP) using several conditionals to define which change should be made, update the string and keep changing the updated string. </p>
  
  <p>For example, start with a string and based on a condition, make a change in the character, then use the second version of the string, and based on another condition, change it some more, and so on, in such a way that at the end of the process, the changes have been cumulative.</p>
  
  <p>Apparently, variable scope prevents the following approach:</p>
  
  <pre><code>$newstring = "This is a test string";
  $value[] // This is an array already defined.
  
   for ($i = 0; $i&lt;=count($value); $i++) {
      switch ($value[$i]) {
       case -1:
        $newstring = preg_replace(// do something with $newstring);
        break;
      case 0:
        $newstring = preg_replace ( // do something else with $newstring);
        break;
      case 1:
        $newstring = substr_replace(//do something else with $newstring);
        break;
      }
   }
  </code></pre>
  
  <p>Is there a way to accomplish this?</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>UPDATE:</strong> Here is my code. As you can expect, <code>$_POST['text1']</code> is a string and <code>$_POST['array']</code> is a two dimensional array.</p>
  
  <pre><code>$text1 = $_POST['text1'];
  $value = $_POST['array'];
  
  for ($i = 0; $i&lt;=count($value); $i++) {
   switch ($value[$i][0]) {
  
  
      case -1:
         $newstring = preg_replace("/".$value[$i][1]."/","",$text1,1);
          break;
      case 0:
          break;
      case 1:
          $newstring = substr_replace($text1, $value[$i][1],$value[$i][2],0);
          break;
      }
  
  }
  </code></pre>