正規表現を用いて文字列中の数字をインクリメントして表示します。
$rep = preg_replace_callback(
"/\d+/",
function($m){
return $m[0]+1;
},
$str // 任意の文字列
);
無名関数で1を加えていきます。
実行した様子は以下の通り。
<?php
<?php
$str = "this year 2019, next year 2020";
$rep = preg_replace_callback(
"/\d+/",
function($m){
return $m[0]+1;
},
$str
);
print($rep); // this year 2020, next year 2021


