這個問題在 PHP 5.3 非常好解決,因為有 anonymous function 可以用,但如果環境還停留在較早以前的 PHP 的話,可以用以下解法
workaround:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
preg_replace_callback( $pattern, array(new rogaCallbackWrapper($a, $b, $c), 'process'), $data); class rogaCallbackWrapper { private $a; private $b; private $c; public function __construct($a, $b, $c) { $this->a = $a; $this->b = $b; $this->c = $c; } public function process(array $matches) { // do something return $this->a . $this->b . $this->c; } } |
anonymous function:
1 2 3 4 5 6 |
preg_replace_callback( $pattern, function ($a) use ($b, $c) { /* do something */ return $a . $b . $c; }, $subject); |
感謝分享喔:>