title: How do I access a variable inside of preg_replace_callback?
body: <p>I'm trying to replace <code>{{key}}</code> items in my <code>$text</code> with values from a passed array. but when I tried adding the <code>print_r</code> to see what was going on I got a <code>Undefined variable: kvPairs</code> error. How can I access my variable form within the <code>preg_replace_callback</code>?</p>
  
  <pre><code>public function replaceValues($kvPairs, $text) {
      $text = preg_replace_callback(
          '/(\{{)(.*?)(\}})/',
          function ($match) {
              $attr = trim($match[2]);
              print_r($kvPairs[strtolower($attr)]);
              if (isset($kvPairs[strtolower($attr)])) {
                  return "&lt;span class='attr'&gt;" . $kvPairs[strtolower($attr)] . "&lt;/span&gt;";
              } else {
                  return "&lt;span class='attrUnknown'&gt;" . $attr . "&lt;/span&gt;";
              }
          },
          $text
      );
      return $text;
  }
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <p>I've tried the <code>global</code> scope thing, but it doesn't work either. I've added 2 print statements to see whats doing on, one inside and one outside the <code>preg_replace_callback</code>.</p>
  
  <pre><code>public function replaceValues($kvPairs, $text) {
      $attrTest = 'date';
      print_r("--" . strtolower($attrTest) . "--" . $kvPairs[strtolower($attrTest)] . "--\n");
      $text = preg_replace_callback(
          '/(\{{)(.*?)(\}})/',
          function ($match) {
              global $kvPairs;
              $attr = trim($match[2]);
              print_r("==" . strtolower($attr) . "==" . $kvPairs[strtolower($attr)] . "==\n");
              if (isset($kvPairs[strtolower($attr)])) {
                  return "&lt;span class='attr'&gt;" . $kvPairs[strtolower($attr)] . "&lt;/span&gt;";
              } else {
                  return "&lt;span class='attrUnknown'&gt;" . $attr . "&lt;/span&gt;";
              }
          },
          $text
      );
      return $text;
  }
  </code></pre>
  
  <p>The output I get is:</p>
  
  <pre><code>--date--1977-05-20--
  ==date====
  </code></pre>