Reputation:
I am currently re-programming a framework in php and I upgraded our development server to php 5.5.3. When I fire up the webbrowser, it returns the following error:
[19-Oct-2013 16:54:05 Europe/Amsterdam] PHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /Applications/MAMP/htdocs/fw/lib/lang.php on line 57
And line 57 is;
$response = preg_replace('/\{'.$find.'(:(.*))?\}/Ue', 'str_replace("{value}", "\\2", $replace)', $response);
I am really horrible at reading these php documentation, I am a beginner and simply changing preg_replace()
by preg_replace_callback()
is too good to be true. A co-worker told me it had to be something like $value[1] but that didn't work eather.
Is there a simple solution, am I overlooking something?
Upvotes: 1
Views: 727
Reputation: 116110
Here is the page about modifiers, giving you more detail about why it is deprecated, and what that means exactly.
Basically the reason is that the /e
modifier causes a string to be evaluated as PHP code, as if eval
was called. Using preg_replace_callback
instead, which allows you to pass it an actual PHP function is indeed the way to go.
If you replace the string-code (second parameter) by an anonymous function, your code should then look something like this:
$response = preg_replace_callback(
'/\{'.$find.'(:(.*))?\}/U',
function($m) use ($replace) {
return str_replace($m, "\\2", $replace);
} ,
$response);
The keyword use
makes sure that the anonymous function can use the $replace
variable which should be defined in the calling scope. See this discussion.
Upvotes: 3