Search code examples
phpzend-frameworksmarty

Error in preg_replace () → preg_replace_callback () rewriting? [Smarty_Compiler.class.php]


After upgrading PHP to version 7, Warning appeared in Smarty_Compiler.class.php.

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /var/www/……/Smarty/Smarty_Compiler.class.php on line 271

Looking at the error location ...... ↓

$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 () seemed to be unusable, and I changed it to 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);

Then, Smarty error appeared this time ... ....

Fatal error: Smarty error: [in PATH]: syntax error: unrecognized tag: php' . str_repeat(" ", substr_count('{*version {$ZEND_VERSION (Smarty_Compiler.class.php, line 458) in /var/www/……/Smarty/Smarty.class.php on line 1095

In the place where the error occurred, the version information is commented out. Before upgrading PHP it worked properly, so I think that I made mistakes in preg_replace_callback () rewriting, but I do not know where wrong ... Also, I'm not sure what this process of Smarty_Compiler.class.php is doing... If you are familiar with PHP or Smarty, please let me know.


Solution

  • Smarty is a template engine, a library, and you shouldn't be modifying its code yourself. Instead try to upgrade the version you use to the newest one. It seems that it's supporting PHP7.