Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

For example ---

If the request value is "Iphone -5" it should be "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;
}

My expected Output ---

Expected Output -Iphone 4

But i am keep getting this error ---

Warning: preg_replace_callback(): Requires argument 2, 'callback', to be a valid callback

Anyone knows how i can fix this issue !!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
252 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...