gpt4 book ai didi

php - 如何更改 preg_match PHP 中的整数值?

转载 作者:行者123 更新时间:2023-12-03 18:50:37 24 4
gpt4 key购买 nike

抱歉,如果我的问题很愚蠢,请有人帮我解决这个问题。

我有这样的字符串

$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/9/";

这个 $str_value 是动态的,它会改变每一页。现在我需要将此字符串中的 9 替换为 10。添加整数 1 并替换

例如,如果 $str_value = "http://99.99.99.99/var/test/src/158-of-box.html/251/"

那么输出应该是

http://99.99.99.99/var/test/src/158-of-box.html/252/

我尝试使用 preg_match 替换,但我出错了,请有人帮助我

$str = preg_replace('/[\/\d+\/]/', '10',$str_value );
$str = preg_replace('/[\/\d+\/]/', '[\/\d+\/]+1',$str_value );

最佳答案

感谢@Calimero 的回答!你比我快,但我也想发布我的答案 ;-)

另一种可能性是使用组来获取整数。所以你不需要修剪 $matches[0] 来删除斜杠。

$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/9/";

$str = preg_replace_callback('/\/([\d+])\//', function($matches) {
return '/'.($matches[1]+1).'/';
}, $str_value);

echo $str;

关于php - 如何更改 preg_match PHP 中的整数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33801167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com