Second parameter in preg_replace_callback()

时光毁灭记忆、已成空白 提交于 2019-12-06 14:52:46

问题


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

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

I hope you can help me!


回答1:


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);
}, ...);

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



来源:https://stackoverflow.com/questions/8735565/second-parameter-in-preg-replace-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!