Passing additional arguments to preg_replace_callback using PHP 5.2.6
<p>I've been researching similar questions, but I'm still a bit unclear if it's possible and/or best way to pass additional arguments in preg_replace_callback using PHP 5.2.6</p>

<p>In this case I'm also looking to pass the $key from the foreach loop along to the if_replace function.</p>

<pre><code>public function output() {
if (!file_exists($this-&gt;file)) {
    return "Error loading template file ($this-&gt;file).&lt;br /&gt;";
}
$output = file_get_contents($this-&gt;file);

foreach ($this-&gt;values as $key =&gt; $value) {
    $tagToReplace = "[@$key]";
    $output = str_replace($tagToReplace, $value, $output);
    $dynamic = preg_quote($key);
    $pattern = '%\[if @'.$dynamic.'\](.*?)\[/if\]%'; // produces: %\[if @username\](.*?)\[/if\]%
    $output = preg_replace_callback($pattern, array($this, 'if_replace'), $output);
}

return $output;
}



public function if_replace($matches) {

    $matches[0] = preg_replace("%\[if @username\]%", "", $matches[0]);
    $matches[0] = preg_replace("%\[/if]%", "", $matches[0]);
    return $matches[0];
}
</code></pre>

<p>Wondering if something like this would work:</p>

<pre><code>class Caller {

public function if_replace($matches) {

    $matches[0] = preg_replace("%\[if @username\]%", "", $matches[0]);
    $matches[0] = preg_replace("%\[/if]%", "", $matches[0]);
    return $matches[0];
}

}

$instance = new Caller;

$output = preg_replace_callback($pattern, array($instance, 'if_replace'), $output);
</code></pre>
