php 已弃用:preg_replace():不推荐使用 /e 修饰符,请改用 preg_replace_callback

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21334934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:03:38  来源:igfitidea点击:

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in

php

提问by Orlo

I need a little help. Because preg_replaceis deprecated, I have to convert all my preg_replaceto preg_replace_callback...

我需要一点帮助。因为preg_replace已弃用,我必须将所有内容转换my preg_replacepreg_replace_callback...

What I've tried:

我试过的:

Change:

改变:

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

To:

到:

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

Error:

错误:

Parse error: syntax error, unexpected 'return' 

回答by rid

The callbackneeds to be a function taking one parameter which is an array of matches. You can pass any kind of callback, including an anonymous function.

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

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

(PHP >= 5.4.0 required in order to use $thisinside the anonymous function)

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