PHP笔记 - Warning: preg_replace_callback(): Requires argument 2

  • 2020-11-15 18:49:59

在一个ECSHOP项目升级到PHP7.X时,发现smarty引擎发出了一个警告,虽然不是致命错误,但是看到一个warning也是很不舒服的

因为是PHP版本升级的缘故出现了这个问题,肯定是PHP7版本抛弃了某个函数造成的。

既然说明了使用 preg_replace_callback 替换,那我们就替换

$source_content = preg_replace_callback ($search.'e', "'"
                                       . $this->_quote_replace($this->left_delimiter) . 'php'
                                       . "' . str_repeat(\"\n\", substr_count('\\0', \"\n\")) .'"
                                       . $this->_quote_replace($this->right_delimiter)
                                       . "'"
                                       , $source_content);

完成后还是有警告,不过警告换成另一个了,函数的参数错了,并不是简单换一个函数就行,里面的参数也要修改。

根据 preg_replace_callback 函数所需参数我们创建一个方法。

function callback_source($matches){
        return "".$this->_quote_replace($this->left_delimiter)."php".str_repeat("n",substr_count("","n"))."".$this->_quote_replace($this->right_delimiter)."";
    }

再把原来的preg_replace函数替换成下面的

$source_content = preg_replace_callback($search,
            array("self","callback_source")
            , $source_content);

问题解决