preg_replace()→preg_replace_callback()重写错误? [Smarty_Compiler.class.php]

时间:2017-09-12 09:17:02

标签: php zend-framework smarty

将PHP升级到版本7后,警告出现在Smarty_Compiler.class.php。

  

警告:preg_replace():不再支持/ e修饰符,请在第271行的/var/www/....../Smarty/Smarty_Compiler.class.php中使用preg_replace_callback

查看错误位置......↓

$source_content = preg_replace($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()似乎无法使用,我将其更改为preg_replace_callback()。

$source_content = preg_replace_callback($search
                                   , function($matches) {
                                         return  "'" 
                                                . $this->_quote_replace($this->left_delimiter) . 'php'
                                                . "' . str_repeat(\"\n\", substr_count('" . $matches[0] . "', \"\n\")) .'"
                                                . $this->_quote_replace($this->right_delimiter)
                                                . "'";
                                     }
                                   , $source_content);

然后,Smarty错误出现了...... ....

  

致命错误:Smarty错误:[在PATH中]:语法错误:无法识别的标签:php' 。 /var/www/....../Smarty/Smarty.class.php中的str_repeat("",substr_count(' {* version {$ ZEND_VERSION(Smarty_Compiler.class.php,第458行))第1095行

在发生错误的位置,版本信息被注释掉。 在升级PHP之前它运行正常,所以我认为我在preg_replace_callback()重写时犯了错误,但我不知道哪里出错... 另外,我不确定Smarty_Compiler.class.php的这个过程是做什么的...... 如果您熟悉PHP或Smarty,请告诉我。

2 个答案:

答案 0 :(得分:1)

Smarty是一个模板引擎,一个库,你不应该自己修改它的代码。而是尝试将您使用的版本升级到最新版本。它似乎支持PHP7。

答案 1 :(得分:1)

试试这个

    $source_content = preg_replace_callback($search,
        function($matches) {
            return
                $this->_quote_replace($this->left_delimiter) . 'php' . str_repeat(
                    $this->_quote_replace("\n"),
                    substr_count($matches[0], $this->_quote_replace("\n"))
                )
                . $this->_quote_replace($this->right_delimiter);
          },
          $source_content
    );