preg_replace_callback()中的第二个参数 [英] Second parameter in preg_replace_callback()

查看:112
本文介绍了preg_replace_callback()中的第二个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中的函数preg_replace_callback()有问题.我想调用一个需要两个参数的函数.

I have a problem with the function preg_replace_callback() in PHP. I want to call a function which requires two parameters.

private function parse_variable_array($a, $b)
{
    return $a * $b;
}

在互联网上,我找到了这段代码:

On the internet I found this piece of code:

preg_replace_callback("/regexcode/", call_user_func_array(array($this, "foo"), array($foo, $bar)), $subject);

但是在foo函数中,我不能使用preg_replace_callback常用的matchs数组

But in the function foo I cannot use the matches array that is usual with a preg_replace_callback

希望你能帮助我!

推荐答案

按原样调用回调,您无法向其传递其他参数.不过,您可以创建一个简单的包装函数.对于PHP 5.3+,使用匿名函数很容易做到这一点:

The callback is called as is, you cannot pass additional parameters to it. You can make a simple wrapper function though. For PHP 5.3+, that's easily done with anonymous functions:

preg_replace_callback(..., function ($match) {
    return parse_variable_array($match, 42);
}, ...);

对于较旧的PHP版本,请像往常一样传递一个常规函数作为回调.

For older PHP versions, make a regular function that you pass as usual as the callback.

这篇关于preg_replace_callback()中的第二个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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