2017-09-19 58 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