来自字符串

时间:2017-12-20 04:50:50

标签: php preg-replace str-replace

我有一个url,我使用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

因此,每当我打开主网址时,应根据主网址更换表格的ID

2 个答案:

答案 0 :(得分:1)

您可以使用全局GET变量

获取id参数
$id = $_GET["id"]

然后你可以根据它更改表格中的网址

$url = "http://anyurl/index.php?id=".$id."&abc=any"

希望这会对你有所帮助

答案 1 :(得分:0)

如果你想在字符串中用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 
?>
相关问题