Replace preg_replace with preg_replace_callback for PHP 5.5

As of PHP 5.5 the preg_replace modifier /e is deprecated. When using it, you will get an deprecated message like E_DEPRECATED >> preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead. The update is quite easy. See the following example:

Old version

$uppercaseValue = preg_replace(
    '/(^|_)([a-z])/e', 
    'strtoupper("\\2")', 
    $string
);

New version

$uppercaseValue = preg_replace_callback(
    '/(^|_)([a-z])/',
    function($matches) { return strtoupper($matches[2]); },
    $string
);

Instead of \\2, the variable we send as parameter to the anonymous function is used as array. In this example $matches[2].

comments powered by Disqus