Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:字符串中的每个数字加1_Php_Regex - Fatal编程技术网

PHP:字符串中的每个数字加1

PHP:字符串中的每个数字加1,php,regex,Php,Regex,我有一根线,像: “交货时间为3-4天。如果选择“快递”,交货时间为1-2天。30天内付款。” 如何在不重新创建字符串的情况下为字符串中的每个数字添加1天?结果应该是: “配送时间为4-5天。如果您选择“快递”,配送时间为2-3天。请在31天内付款。”您可以使用: <?php $s = "The delivery time will be 3-4 days. If you choose 'express' the delivery time will be 1-2 days. Pay w

我有一根线,像: “交货时间为3-4天。如果选择“快递”,交货时间为1-2天。30天内付款。”

如何在不重新创建字符串的情况下为字符串中的每个数字添加1天?结果应该是: “配送时间为4-5天。如果您选择“快递”,配送时间为2-3天。请在31天内付款。”

您可以使用:

<?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;

?>

您可以使用:

<?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;

?>


您尝试了什么?有代码要共享吗?31天怎么样?应该转到32吗?
preg_replace_回调('/\d+/',函数($n){return intval($n)+1;},$input)哇那很难:DIt是
30
而不是
31
@Mihai@NiettheDarkAbsol~
返回intval($n)+1将不起作用,但
返回intval($n[0])+1preg_replace_回调('/\d+/',函数($n){return intval($n)+1;},$input)哇那很难:DIt是
30
而不是
31
@Mihai@NiettheDarkAbsol~
返回intval($n)+1将不起作用,但
返回intval($n[0])+1你为什么要复制我的答案?@baao,没有,先生,我正在写,在发布之前没有看到你的答案,这个页面已经打开了。但它确实匹配。对不起,你为什么抄我的答案?@baao,没有,先生,我正在写,在发布之前没有看到你的答案,这个页面已经打开了。但它确实匹配。很抱歉