我有一个字符串:"交货时间为3-4天.如果您选择'快递',交货时间将为1-2天.在30天内付款."
如何在不重新创建字符串的情况下为字符串中的每个数字添加1天?结果应该是:"交货时间为4-5天.如果您选择'快递',交货时间将为2-3天.在31天内付款."
你可以preg_replace_callback
这样做:
<?php
$s = "The delivery time will be 3-4 days. If you choose 'express' the delivery time will be 1-2 days. Pay within 30 days.";
function callback($matches) {
return $matches[0] + 1;
}
$pattern = '~([0-9]+)~';
$r = preg_replace_callback($pattern, 'callback', $s);
echo $r;
?>
Run Code Online (Sandbox Code Playgroud)