【问题标题】:PHP preg_replace: Deprecated [duplicate]PHP preg_replace:已弃用[重复]
【发布时间】:2014-04-29 20:43:16
【问题描述】:

将我的 php 切换到 5.5.8 后,我收到了这个已弃用的警告,

不推荐使用:preg_replace():不推荐使用 /e 修饰符,请使用 preg_replace_callback 而不是在 C:\wamp\www...Curly.php 上 第 28 行

这是我Curly类中的函数,

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

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

所以如果我改用preg_replace_callback

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

然后我得到这个错误,

警告:preg_replace_callback():需要参数 2, '$this->_replace("\1",$options)',作为一个有效的回调 在 C:\wamp\www...Curly.php28

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

编辑:

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>";
    }
}

【问题讨论】:

  • 查看documentation 中的示例,第二个参数必须是函数,而不是字符串。
  • 我在上一个答案中的解释也可能有用:stackoverflow.com/questions/15454220/…
  • 事实上,我认为这实际上是该问题的重复,因为基本用法保持不变。
  • 谢谢 IMSop,我已经尝试过这个答案,但是我得到了错误并且根本没有发生替换。
  • @tealou 然后请显示您根据该答案编写的代码,以及您遇到的错误

标签: php preg-replace preg-replace-callback


【解决方案1】:

否则使用 call_user_func_array

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

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

    // Data
    $input
);

【讨论】:

  • 我不确定 call_user_func_array 与任何事情有什么关系。除非修改 _replace 函数,否则此代码也会失败,因为它需要一个字符串,但会将正则表达式中的所有匹配项作为数组提供。
  • call_user_func_array 此时将被调用。
  • 不,不会。这只是在 PHP 中表示对象回调的标准方式。 call_user_func_array 中的“数组”不是指回调的类型,而是它给出的参数,这些参数是从您提供的数组中提取的,就好像您将它们列为单独的参数一样。这里没有发生这样的事情。
【解决方案2】:

怎么样:

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

【讨论】:

  • 我喜欢匿名函数 :)
  • 您的匿名函数实际上并没有执行替换,它只会在字符串中间插入一些 PHP 代码。
  • @IMSoP:会的。可能是我错了,但我想这是 OP 想要的。
  • 已弃用的 /e 修饰符使 preg_replace eval() 成为其参数。请参阅我对上面链接的类似问题的回答(可能相似到足以考虑重复)。
  • @tealou: 调用回调的时候要加上use($options)。查看我的编辑。
猜你喜欢
  • 2013-10-15
  • 2020-04-20
  • 2011-05-12
  • 1970-01-01
  • 2013-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
相关资源
最近更新 更多