Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 不推荐:preg\u replace():如何转换为preg\u replace\u回调?_Php - Fatal编程技术网

Php 不推荐:preg\u replace():如何转换为preg\u replace\u回调?

Php 不推荐:preg\u replace():如何转换为preg\u replace\u回调?,php,Php,如何将以下preg_replace转换为preg_replace_回调 $this->template = preg_replace ( "#\\[group=(.+?)\\](.*?)\\[/group\\]#ies", "\$this->check_group('\\1', '\\2')", $this->template ); 我所尝试的: $this->template = preg_replace_callback(

如何将以下preg_replace转换为preg_replace_回调

            $this->template = preg_replace ( "#\\[group=(.+?)\\](.*?)\\[/group\\]#ies", 
 "\$this->check_group('\\1', '\\2')", $this->template );
我所尝试的:

        $this->template = preg_replace_callback( "#\\[not-group=(.+?)\\](.*?)\\[/not-group\\]#ies",
                function($this) {
                        return $this->check_group($this[1], $this[2], false);
                }
        , $this->template );

上面的preg_replace_回调给了我一个空结果。

因为缺少分号
在那里

return $this->check_group($this[1], $this[2], false);
                                             -------^ // Here

因为您缺少分号
在那里

return $this->check_group($this[1], $this[2], false);
                                             -------^ // Here

不要在preg\u replace\u callback()调用中使用\e修饰符,否则php将抛出以下警告,而不返回任何内容:

PHP警告:preg_replace_callback():修饰符/e不能用于 第xx行/where/you/used/it.php中的替换回调


另外,只是一个建议,不要在回调函数中使用$this作为参数名。。。这太令人困惑了。

不要在preg\u replace\u callback()调用中使用\e修饰符,否则php将抛出以下警告而不返回任何内容:

PHP警告:preg_replace_callback():修饰符/e不能用于 第xx行/where/you/used/it.php中的替换回调


另外,只是一个建议,不要在回调函数中使用$this作为参数名。。。这太令人困惑了。

为了在上下文中正确使用“$this”,您需要为匿名函数提供use关键字。此外,$这是一个特殊变量,直接将其用作函数参数是不好的做法。同样在匿名函数中,您试图使用$this作为匹配的变量,我将用更具描述性的变量“$matches”替换函数参数中的$this。看看下面是否解决了您的问题

    $this->template = preg_replace_callback( "#\\[not-group=(.+?)\\](.*?)\\[/not-group\\]#is",
            function($match) use ($this) {
                    return $this->check_group($match[1], $match[2], false);
            }
    , $this->template );

为了在上下文中正确使用“$this”,您需要为匿名函数提供use关键字。此外,$这是一个特殊变量,直接将其用作函数参数是不好的做法。同样在匿名函数中,您试图使用$this作为匹配的变量,我将用更具描述性的变量“$matches”替换函数参数中的$this。看看下面是否解决了您的问题

    $this->template = preg_replace_callback( "#\\[not-group=(.+?)\\](.*?)\\[/not-group\\]#is",
            function($match) use ($this) {
                    return $this->check_group($match[1], $match[2], false);
            }
    , $this->template );

你能更新问题中的测试数据和预期输出吗?在php5.5中,你似乎不能使用e修饰符。将
#ies
更改为
#isu
,并且它现在正在工作。您可以更新测试数据和问题中的预期输出吗?在php5.5中,您似乎不能使用e修饰符。已将
#ies
更改为
#isu
,它现在正在工作