如何将这个`create_function`调用转换为匿名函数? [英] How do I convert this `create_function` call to an anonymous function?

查看:75
本文介绍了如何将这个`create_function`调用转换为匿名函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用 create_function 的代码,现已弃用. create_function 的注释说 * @deprecated 7.2改用匿名函数.

I've got some code that uses create_function, which is now deprecated. The comments for create_function say * @deprecated 7.2 Use anonymous functions instead.

这是函数本身: create_function('$ matches','return strtoupper('\ 0");')

这是上下文:

$match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./',
                    create_function ('$matches', 'return strtoupper("\0");'), strtolower(trim($match[1])));

我的代码未运行,因为它已被弃用,因此我想将其转换为匿名函数.我不完全确定这段代码的作用,所以我实际上没有可靠的测试方法,但是我认为可以用create_function块替换

My code isn't running because it's been deprecated, so I want to turn it into an anonymous function instead. I'm not entirely sure what this bit of code does, so I don't really have a reliable way of testing it, but I THINK that create_function block can be replaced with

function($matches) {
    return strtoupper($matches);
}

那行得通吗?这样做与 create_function 函数具有相同的作用吗?我不确定在这种情况下"\ 0&" 会做什么,所以我希望可以得到一些建议.

Would that work? Does that do the same thing that the create_function function does? I'm not sure what "\0" does in that context, so I'm hoping I can get some advice.

推荐答案

我在另一个地方找到了类似的问题,并在那里尝试了答案.原来它等效于以下功能:

I found a similar question another place and tried the answer there. Turns out it's equivalent to this function:

function($m) {
   return strtoupper($m[0]);
}

在这里找到它:不推荐使用:preg_replace():不推荐使用/e修饰符,请使用preg_replace_callback

感谢 @Philipp

这篇关于如何将这个`create_function`调用转换为匿名函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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