How to convert preg_replace e to preg_replace_callback?

Okay, so I’m slightly confused. Here is the code I have now, but I just found out the e modifier is deprecated. How do I convert it to a preg_replace_callback()? I still haven’t figured it out.

$post = preg_replace("/\[code\]([^] )\[\/code\]/e", 'code(\'$1\')', $post);

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

If memory serves, preg_replace_callback() gives you the results of a $match from preg_match() as input, and expects the final result as output. So you’d need to write a function that returns e.g. "code('{$match[1]}')".

It can be an inline function, naturally, if php 5.3 is an option:

preg_replace_callback($regex, function($match) {
  // do stuff
  return $stuff;
}, $subject);

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply