Php preg_最后替换为斜杠

Php preg_最后替换为斜杠,php,preg-replace,Php,Preg Replace,有这个密码吗 <?php $string = "list page.php?cpage=1, list page.php?cpage=2, list page.php?page=3 thats all"; $string = preg_replace("/\?cpage=[0-9]/", "/", $string); echo $string; //result //list page.php/, list page.php/, list page.php/ thats all //w

有这个密码吗

<?php

$string = "list page.php?cpage=1, list page.php?cpage=2, list page.php?page=3 thats all";
$string = preg_replace("/\?cpage=[0-9]/", "/", $string);
echo $string;
//result 
//list page.php/, list page.php/, list page.php/ thats all
//what i want 
//list page.php/1/, list page.php/2/, list page.php/3/ thats all
?>

有人能帮忙吗

演示

捕获组1中的
“3”
,替换中的
/$1/
作为
“/3/”
捕获()之间的值,并通过$1将其投影回:

$string = "list page.php?cpage=1, list page.php?cpage=2, list page.php?page=3 thats all";
$string = preg_replace("/\?c?page=([0-9]{1,})/", "/$1/", $string);
echo $string;
([0-9]{1,})
表示一个或多个数字。
希望这有帮助。

您有什么问题吗?
preg_replace("/\?c?page=([0-9]+)/", "/$1/", "page.php?cpage=3");
$string = "list page.php?cpage=1, list page.php?cpage=2, list page.php?page=3 thats all";
$string = preg_replace("/\?c?page=([0-9]{1,})/", "/$1/", $string);
echo $string;