title: Replacing Placeholder Variables in a String
body: <p>Just finished making this function. Basically it is suppose to look through a string and try to find any placeholder variables, which would be place between two curly brackets <code>{}</code>. It grabs the value between the curly brackets and uses it to look through an array where it should match the key. Then it replaces the curly bracket variable in the string with the value in the array of the matching key.</p>
  
  <p>It has a few problems though. First is when I <code>var_dump($matches)</code> it puts puts the results in an array, inside an array. So I have to use two <code>foreach()</code> just the reach the correct data.</p>
  
  <p>I also feel like its heavy and I've been looking over it trying to make it better but I'm somewhat stumped. Any optimizations I missed?</p>
  
  <pre><code>function dynStr($str,$vars) {
      preg_match_all("/\{[A-Z0-9_]+\}+/", $str, $matches);
      foreach($matches as $match_group) {
          foreach($match_group as $match) {
              $match = str_replace("}", "", $match);
              $match = str_replace("{", "", $match);
              $match = strtolower($match);
              $allowed = array_keys($vars);
              $match_up = strtoupper($match);
              $str = (in_array($match, $allowed)) ? str_replace("{".$match_up."}", $vars[$match], $str) : str_replace("{".$match_up."}", '', $str);
          }
      }
      return $str;
  }
  
  $variables = array("first_name"=&gt;"John","last_name"=&gt;"Smith","status"=&gt;"won");
  $string = 'Dear {FIRST_NAME} {LAST_NAME}, we wanted to tell you that you {STATUS} the competition.';
  echo dynStr($string,$variables);
  //Would output: 'Dear John Smith, we wanted to tell you that you won the competition.'
  </code></pre>