Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

php - 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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...