列如 一个文本 中的字符串为
$text = "April fools day is 04/01/2002,Last christmas was 12/24/2001,April fools day is 04/01/2002, Last christmas was 12/24/2001, April fools day is 04/01/2002, Last christmas was 12/24/2001, April fools day is 04/01/2002, Last christmas was 12/24/2001, April fools day is 04/01/2002, Last christmas was 12/24/2001, April fools day is 04/01/2002, Last christmas was 12/24/2001, April fools day is 04/01/2002, Last christmas was 12/24/2001, April fools day is 04/01/2002, Last christmas was 12/24/2001, April fools day is 04/01/2002, Last christmas was 12/24/2001, April fools day is 04/01/2002, Last christmas was 12/24/2001, April fools day is 04/01/2002, Last christmas was 12/24/2001" ,如何将这个字符串变量中的 所有年份增加一年?
$str = preg_replace_callback("|(\d{2}/\d{2}/)(\d{2})|",function($matches){ return $matches[1].($matches[2] + 1) },$text);
意思是,将$text中满足表达式"|(\d{2}/\d{2}/)(\d{4})|" 的内容放进闭包函数next_year中并进行运算
从|(\d{2}/\d{2}/)(\d{4})| 中可以看出 表达式 有 两个括号,则 匿名函数将会传入满足正则表达式的数组,如下:
Array | |
( | |
[0] => 04/01/2002 | |
[1] => 04/01/ | |
[2] => 2002 | |
) |
此时我们可以在匿名函数返回想要的内容 即 $matches[1].($matches[2]+1)