Tag: smarty

Smarty 2 in PHP 5.5

When running a PHP powered website using Smarty 2 on a PHP 5.5 webserver it shows a deprecation error. This is caused by a deprecation of the /e parameter in the preg_replace function. Fortunately on the Smarty forum a topic is
available that provides a solution, without having to lower error_reporting in the server configuration.

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in xxx/xxxx/php5/smarty/base/Smarty_Compiler.class.php on line 270 

The original code on line 270 of _Smarty_Compiler.php is:

/* replace special blocks by "{php}" */
$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);

The culprit here is the /e modifier (PREG_REPLACE_EVAL)

The replacement to remove the deprecation notice is to use a preg_replace_callback:

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

Leave a Comment