PHP preg_替换:已弃用

PHP preg_替换:已弃用,php,preg-replace,preg-replace-callback,Php,Preg Replace,Preg Replace Callback,在将php切换到5.5.8之后,我收到了这个不推荐使用的警告 不推荐使用:preg_replace():不推荐使用/e修饰符,请使用 替换C:\wamp\www…Curly.php上的preg\u replace\u回调 第28行 这是我的Curly类中的函数 public function replace ($input, $options = array()) { return preg_replace("/\{{2}(([a-z\_]+\|.+)|([a-z\_]+))\

在将php切换到5.5.8之后,我收到了这个不推荐使用的警告

不推荐使用:preg_replace():不推荐使用/e修饰符,请使用 替换C:\wamp\www…Curly.php上的preg\u replace\u回调 第28行

这是我的
Curly
类中的函数

public function replace ($input, $options = array()) {

        return preg_replace("/\{{2}(([a-z\_]+\|.+)|([a-z\_]+))\}{2}/Ue",'$this->_replace("\\1",$options)',$input);
    }
因此,如果我使用
preg\u replace\u callback

return preg_replace_callback("/\{{2}(([a-z\_]+\|.+)|([a-z\_]+))\}{2}/Ue",'$this->_replace("\\1",$options)',$input);
然后我得到这个错误

警告:preg_replace_callback():需要参数2, “$this->\u replace(“\1”,$options)”,作为有效回调 在第28行的C:\wamp\www…Curly.php中

有什么办法可以解决这个问题吗

编辑:

class Curly extends CoreModel
{
    // Set the property/ variable of this class
    public $constant = null;

    /**
     * Extend the parent class property.
     */ 
    public function __construct($connection){

       // Extend parent's.
       parent::__construct($connection);
       $this->constant = new Constant($connection);
    }

    /**
     * Replace the curly in an input string
     * @param string $input
     * @return string
     */
    public function replace ($input, $options = array()) {

        //return preg_replace("/\{{2}([a-z]+\|.+)\}{2}/Ue",'$this->_replace("\\1")',$input);
        //return preg_replace("/\{{2}(([a-z\_]+\|.+)|([a-z\_]+))\}{2}/Ue",'$this->_replace("\\1",$options)',$input);
        return preg_replace_callback(
            "/\{\{([a-z_]+(?:\|.+)?)\}\}/U",
            function($m) { return $this->_replace($m[1], $options); }, 
            $input
        );
    }

    /**
     * Run the replacement code on a given macro string
     * @param string $input
     * @return string
     */
    private function _replace ($input,$options) {

        // Set local vars.
        $defaults = array();

        // Call internal method to process the array.
        $array = parent::arrayMergeValues($defaults,$options);
        //print_r($array);

        // Convert array to object.
        $property = parent::arrayToObject($array);

        // type-checking comparison operator is necessary.
        if (strpos($input, '|') !== false) { 

            //VERTICAL SIGN FOUND
            list ($name,$params) = explode("|",$input);

            if (method_exists($this,$name)) {
                return $this->$name($params);
            }
            throw new Exception ("Unrecognised macro: {$name}.",500);

        } else {

            // Get the input string and request the data from constant table.
            $value = $this->constant->getRow($input)->value;

            // If there is a value returned from the contstant table.
            if($value !== null) { 

                // Return the what is returned from the the constant table.
                return $value;

            } else if(isset($property->$input)) { // If there is a customised value from the developer.

                // Return what is customised by the developer.
                return $property->$input;

            } else { // Nothing is found.

                // Return what is from the input.
                return "{{{$input}}}"; 

            }

        }   
    }

    /**
     * Replaces a YouTube curly
     * @param string $params
     * @return string
     */
    private function youtube ($params) {

        parse_str($params);

        // set defaults
        if (!isset($id)) { $id = "ykwqXuMPsoc"; }
        if (!isset($width)) { $width = 560; }
        if (!isset($height)) { $height = 315; }

        // output the final HTML
        return "<iframe width=\"{$width}\" height=\"{$height}\" src=\"http://www.youtube.com/embed/{$id}\" frameborder=\"0\" allowfullscreen></iframe>";
    }
}
类Curly扩展了CoreModel
{
//设置此类的属性/变量
公共$constant=null;
/**
*扩展父类属性。
*/ 
公共功能构造($connection){
//扩展家长的。
父项::_构造($connection);
$this->constant=新常量($connection);
}
/**
*替换输入字符串中的卷曲
*@param字符串$input
*@返回字符串
*/
公共函数替换($input,$options=array()){
//返回preg\u replace(“/\{2}([a-z]+\\{124;.+)\}{2}/Ue“,“$this->u replace(\\1”),$input);
//返回preg\u replace(“/\{2}”([a-z\\\\\+\\\\\\\\\.+)\\\{2}/Ue“,“$this->u replace(\\1,$options)”,$input);
返回preg_replace_回调(
“/\{([a-z\]+(?:\\\.+)?)\}}/U”,
函数($m){return$this->u replace($m[1],$options);},
$input
);
}
/**
*在给定的宏字符串上运行替换代码
*@param字符串$input
*@返回字符串
*/
私有函数\u替换($input$选项){
//设置本地变量。
$defaults=array();
//调用内部方法来处理数组。
$array=parent::arrayMergeValues($defaults,$options);
//打印(数组);
//将数组转换为对象。
$property=parent::arrayToObject($array);
//类型检查比较运算符是必需的。
if(strpos($input,“|”)!==false){
//发现垂直标志
列表($name,$params)=分解(“|”,$input);
如果(方法_存在($this$name)){
返回$this->$name($params);
}
抛出新异常(“无法识别的宏:{$name}.”,500);
}否则{
//获取输入字符串并从常量表请求数据。
$value=$this->constant->getRow($input)->value;
//如果有一个值从contstant表返回。
如果($value!==null){
//返回从常量表返回的内容。
返回$value;
}else if(isset($property->$input)){//如果开发人员有自定义值。
//返回开发人员自定义的内容。
返回$property->$input;
}否则{//什么也找不到。
//返回输入中的内容。
返回“{{$input}}}”;
}
}   
}
/**
*替换YouTube卷发
*@param字符串$params
*@返回字符串
*/
私有函数(参数){
parse_str($params);
//设置默认值
如果(!isset($id)){$id=“ykwqXuMPsoc”;}
如果(!isset($width)){$width=560;}
如果(!isset($height)){$height=315;}
//输出最终的HTML
返回“”;
}
}

否则使用调用用户函数数组

return preg_replace_callback(
    // RegExpr
    "/\{{2}(([a-z\_]+\|.+)|([a-z\_]+))\}{2}/Ue",

    // Callback
    array($this, '_replace'),

    // Data
    $input
);
那么:

public function replace ($input, $options = array()) {
    return preg_replace_callback(
        "/\{\{([a-z_]+(?:\|.+)?)\}\}/U",
        function($m) use($options) { return $this->_replace($m[1], $options); }, 
        $input
    );
}

请看第二个参数中的示例,第二个参数必须是函数,而不是字符串。我在前面的回答中的解释可能也很有用:事实上,我认为这实际上是该问题的重复,因为基本用法保持不变。感谢IMSop,我已经尝试过该答案,但是我得到了一个错误,替换根本没有发生。@tealou然后请显示您根据该答案编写的代码,以及您得到的错误。我不确定call\u user\u func\u数组与任何事情有什么关系。除非修改_replace函数,否则此代码也将失败,因为它需要一个字符串,但将以数组形式提供正则表达式中的所有匹配项。
call\u user\u func\u array
此时将被调用。不,不会。这只是PHP中表示对象回调的标准方式。call_user_func_数组中的“array”不是指回调的类型,而是指它给定的参数,这些参数是从您提供的数组中提取的,就像您将它们作为单独的参数列出一样。这里没有发生类似的事情。我喜欢匿名函数:)您的匿名函数实际上并没有执行替换,它只会在字符串中间插入一些PHP代码。@IMSoP:是的,它会。也许我错了,但我想这是OP想要的。不推荐使用的
/e
修饰符使preg_replace
eval()
成为其参数。请参见我对上面链接的类似问题的回答(可能类似于考虑重复)。@当调用回调时,必须添加<代码>使用($选项)< /C>。请参阅我的编辑。