2017-09-19 56 views
-1

我想这个函数来preg_replace_callback但几乎我都试过了转换提供了错误:套牢了preg_replace()来Preg_replace_callback()

需要参数2,“$ DB->模块”,是在

这是一个有效的回调是我的代码:?

$this->template = preg_replace ("/#module\=(\w+)#/ie", "\$this->module('\\1')", $this->template); 

任何想法如何将它转换..

+0

回调函数是一个函数,关于http://php.net/manual/en/function.preg-replace-callback.php – Calimero

+0

应仔细选择哪些参数和返回值我将重新打开这个问题,因为它是一个在这种情况下需要使用类方法的回调函数。 –

回答

2

我特别回答这个问题,因为你必须在其中使用类方法。所以它并不比关于这个主题的百万个答案简单。

一种方式做到这一点,改变的方式图案整个匹配是yourclass::module参数,并通过用$this阵列,并且该方法名作为第二个参数:

$this->template = preg_replace_callback('/#module=\K\w+(?=#)/i', array($this, module), $this->template); 

$this->template = preg_replace_callback('/#module=\K\w+(?=#)/i', 'self::module', $this->template); 

其他方法,保持相同模式并使用$that=$this;绝招:

$that = $this; 
$this->template = preg_replace_callback('/#module=(\w+)#/i', function ($m) use ($that) { 
    return $that->module($m[1]); 
}, $this->template); 
+0

非常感谢你! – Dikobraz