PHP preg_replace_callback, replace only 1 backreference?

后端 未结 3 2029
一个人的身影
一个人的身影 2021-01-20 05:12

Using preg_replace_callback, is it possible to replace only one backreference? Or do I have to return the entire thing?

I\'m just trying to wrap the tok

3条回答
  •  天涯浪人
    2021-01-20 06:09

    I recently came up with a way more simple way of doing this. For example; if I want to match \w+\d+\w+ and only change the digits.

    $value = preg_replace_callback('~(\w+)(\d+)(\w+)~', function($match) {
        $match[2] = $match[2] * 2;//Do whatever I want to $match[2]
        return $match[1] . $match[2] . $match[3];
    }, $value);
    

    Very clean!

提交回复
热议问题