preg_replace_callback():需要参数2 PHP [英] preg_replace_callback(): Requires argument 2 PHP
本文介绍了preg_replace_callback():需要参数2 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用preg_replace_callback,但我一直遇到错误。
i am trying to use the preg_replace_callback but i am keep getting an error.
我的目标是输入中是否包含(-),所以它
My goal is to if there is any (-) in the input, so it actually -1 value from the input.
例如---
如果请求值是 Iphone -5,应该是 Iphone 4
public function justTestAction($string) {
$string = "Iphone -5";
$result = preg_replace_callback('/-\d+/', 'callback', $string);
function callback($matches) {
return abs($matches[0]) - 1;
}
return $result;
}
我的预期输出---
Expected Output -Iphone 4
但是我我一直在收到此错误---
But i am keep getting this error ---
警告:preg_replace_callback():要求参数2'callback'为
有效的回调
Warning: preg_replace_callback(): Requires argument 2, 'callback', to be a valid callback
任何人都知道我如何解决此问题!!!
Anyone knows how i can fix this issue !!!
推荐答案
这是因为您的回调
函数位于类方法中。如果将代码更改为此,它应该可以正常工作:
It's because your callback
function is in a class method. If you change your code to this it should work fine:
public function justTestAction($string) {
$string = "Iphone -5";
$result = preg_replace_callback('/-\d+/', function($matches){
return abs($matches[0]) - 1;
}, $string);
return $result;
}
这篇关于preg_replace_callback():需要参数2 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文