我有一个网址,我使用GET方法获取值,我想用字符串中的4位随机数替换该值
这是我的主要网址:http://localhost/ab/index.php?id=3345
这些是我的表中的字符串(从数据库中获取):
http://anyurl/index.php?id=4876&abc=any
http://anyurl/index.php?id=8726&abc=any
http://anyurl/index.php?id=9026&abc=any
因此,每当我打开主URL时,应根据主URL更换表的id
你可以使用全局GET变量获取id参数
$id = $_GET["id"]
然后你可以根据它更改表格中的网址
$url = "http://anyurl/index.php?id=".$id."&abc=any"
希望对你有帮助
如果你想用字符串中的preg_replace
替换id,那么你可以这样做:
<?php
$string = 'http://anyurl/index.php?id=4876&abc=any';
$new_string = preg_replace('/[0-9]+/', $_GET["id"], $string);
echo $new_string;
// Will display http://anyurl/index.php?id=3345&abc=any
?>