How can I find, increment and replace in php?

前端 未结 3 1057
死守一世寂寞
死守一世寂寞 2021-01-23 01:03

I have strings in the form \\d+_\\d+ and I want to add 1 to the second number. Since my explanation is so very clear, let me give you a few examples:

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-23 01:42

    Here's the solution for the PHP 5.3 (now when PHP supports lambdas)

    $new = preg_replace_callback("/(\d+_)(\d+)", function($matches)
    {
        return $matches[1] . (1 + $matches[2]);
    }
    , $new);
    

提交回复
热议问题