Help to update a deprecated php code

I know I may sound dumb, but can someone help with this pice of code

function __($text, $array) {
        $text = preg_replace("/\{(\w+)\}/e", "\$array[strtolower('$1')]", $text);
        return $text;
}

It is in a function file. I kept receiving an error saying “preg_replace(): The /e modifier is deprecated, use preg_replace_callback”

Any good answer will be highly appreciated.

Jean Antoine

The change is pretty straight-forward -

  1. Change the function name from preg_replace to preg_replace_callback.
  2. Remove the e modifier.
  3. Change the 2nd parameter, with the php code that was getting eval(uated), to an anonymous/in-line function that returns the same value.
function __($text, $array) {
        $text = preg_replace_callback("/\{(\w+)\}/",
		function ($matches) use ($array) {
            return $array[strtolower($matches[1])];
        }
		, $text);
        return $text;
}

Thanks. It works.

Keep up the good work.

Jean Antoine

Sponsor our Newsletter | Privacy Policy | Terms of Service