Magento – Magento PHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

magento-1.9PHP

I'm in the process of updating a Magento site and ran into a error that is causing me to delay pushing this to live.

PHP error:

PHP Deprecated:  preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /skin/frontend/default/galadrugstore/css/style.php on line 62

Style.php line 62:

$content = preg_replace('/\$([\w]+)/e','$0',@file_get_contents($stylesheet));

I have tried the following with out any traction, as a last resort I wanted to see if someone could assist before I lose my mine.. lol

$content = preg_replace_callback('/\$([\w]+)', function($match) { return CallFunction($match[0]); }, @file_get_contents($stylesheet)); 
$content = preg_replace_callback('/\$([\w]+)/', function($match) { return CallFunction($match[0]); }, @file_get_contents($stylesheet));
$content = preg_replace_callback('/\$([\w]+)/', function($match) { return $match[0]; }, @file_get_contents($stylesheet));

I feel I'm really close but something is just not right, any assistance with this issue would be awesome.

Big Thanks in Advance!

Best Answer

The e modifier is short for eval. It performs a back reference substitution, then executes the replacement string as PHP, then performs the substitution using the returned value.

For example:

$pattern     = '/(.*)/e';
$replacement = 'strtoupper("$0");';
$input       = 'hello';
echo preg_replace($pattern, $replacement, $input);

Can be translated into:

$pattern     = '/(.*)/';
$input       = 'hello';
echo preg_replace_callback($pattern, function ($matches) {
    return strtoupper($matches[0]);
}, $input);

Without knowing what's returned by @file_get_contents($stylesheet) it's a little difficult to give accurate advice. The most obvious answer is the following, however be warned, this is pretty much as insecure as the e modifier (which is why it's deprecated):

echo preg_replace_callback('/\$([\w]+)/', function ($matches) {
    return eval($matches[0]);
}, $input);