title: PHP preg_match_all search and replace
body: <p>I have tried about a million different regexes and I just can't wrap my head around this one (admittedly a lot of regex is out of my grasp).</p>
  
  <p>In my text I have variables like this:</p>
  
  <pre><code>{{$one}}
  {{$three.four.five}}
  {{$six.seven}}
  </code></pre>
  
  <p>And I have an array with all the replaces for them (the index for 'one' is 'one' etc) but some may be missing.</p>
  
  <p>I want to replace from the array if it exists and if not leave the text alone.</p>
  
  <p>What regex can I use to preg_match_all of the variables in $text in the snippet below, replace from $replace where appropriate and echo out to the browser?</p>
  
  <pre><code>&lt;?php
      $replaces = array('testa.testb.testc' =&gt; '1', 'testc.testa' =&gt; '2', 'testf' =&gt; '3');
      $text = '{{$testa.testb.testc}}&lt;br&gt;{{$testc.testa}}&lt;br&gt;{{$testf}}&lt;br&gt;{{$aaaaa}}&lt;br&gt;';
  
      preg_match_all('/\{\{\$(\w+)\}\}/e', $text, $matches);
  
      foreach($matches as $match)
      {
          $key = str_replace('{{$', '', $match);
          $key = str_replace('}}', '', $key);
  
          if(isset($replaces[$key]))
              $text = str_replace($match, $replaces[$key], $text);
      }
  
      // I want to end up echo'ing:
      //   1&lt;br&gt;2&lt;br&gt;3&lt;br&gt;{{$aaaaa}}&lt;br&gt;
  
      echo $text;
  ?&gt;
  </code></pre>
  
  <p><a href="http://codepad.viper-7.com/4INvEE" rel="noreferrer">http://codepad.viper-7.com/4INvEE</a></p>
  
  <p>This:</p>
  
  <pre><code>'/\{\{\$(\w+)\}\}/e'
  </code></pre>
  
  <p>like in the snippet, is the closest I have gotten.</p>
  
  <p>It has to work with the does in the variable names too.</p>
  
  <p>Thanks in advance for all the help!</p>