怎么把preg_replace e转换成preg_replace_callback? [英] How to convert preg_replace e to preg_replace_callback?

查看:120
本文介绍了怎么把preg_replace e转换成preg_replace_callback?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有点困惑.这是我现在拥有的代码,但是我刚刚发现e修饰符已被弃用.如何将其转换为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);

推荐答案

如果提供内存,则preg_replace_callback()会将preg_match()$match结果作为输入,并期望最终结果作为输出.因此,您需要编写一个返回例如"code('{$match[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]}')".

如果可以选择php 5.3,它自然可以是一个内联函数:

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

这篇关于怎么把preg_replace e转换成preg_replace_callback?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆