不推荐使用:preg_replace():不推荐使用/ e修饰符,而是使用preg_replace_callback

Orl*_*rlo 8 php

我需要一些帮助.因为preg_replace已弃用,我必须全部转换my preg_replacepreg_replace_callback...

我尝试过的:

更改:

$template = preg_replace ( "#\\[aviable=(.+?)\\](.*?)\\[/aviable\\]#ies", "\$this->check_module('\\1', '\\2')", $template );
Run Code Online (Sandbox Code Playgroud)

至:

$template = preg_replace_callback ( "#\\[aviable=(.+?)\\](.*?)\\[/aviable\\]#isu", 
                return $this->check_module($this['1'], $this['2']);
            $template );
Run Code Online (Sandbox Code Playgroud)

错误:

Parse error: syntax error, unexpected 'return' 
Run Code Online (Sandbox Code Playgroud)

rid*_*rid 9

回调必须采取一个参数,该参数是匹配的数组的函数.您可以传递任何类型的回调,包括匿名函数.

$template = preg_replace_callback(
    "#\\[aviable=(.+?)\\](.*?)\\[/aviable\\]#isu",
    function($matches) {
        return $this->check_module($matches[1], $matches[2]);
    },
    $template
);
Run Code Online (Sandbox Code Playgroud)

(为了$this在匿名函数中使用,需要PHP> = 5.4.0 )

  • @ArlaudPierre,我认为没错.如果您不需要,可以创建第二个变量? (2认同)