PHP - "use($var)" not working in preg_replace_callback

55 views Asked by At

I'm trying to pass variable from one function to another and then to preg_replace_callback:

class Renderer
{
private static function make_query($q) {

    ...

    while ($r = mysqli_fetch_array($c)) {
        $data = $r['title'];
        $finaltpl.=self::render($q[0]['template'],$data);
    }

    public static function render ($render,$d='') {
        echo 'var D is:'.$d.'<br>';
        $render = preg_replace_callback("/\[\[\[([^\]]*)\]\]\]/", function ($matches) use ($d) {
        return 'var D inside is:'.$d.'<br>';
            },$render
            );
    return $render;
}

But outside the replacer it works normally and echoes the var correctly, but inside it just shows $d as an empty string. Where can the problem be?

0

There are 0 answers