我正在将一个URL作为参数传递到下一页。 ?url=http://domain.com 我想为查询字符串或 URL 设置其他参数。 但前提是查询字符串中存在特定域。我试过了
$url = preg_replace('{http://www.domain.com}','http://www.domain.com?foo=bar/',$_GET['url']);
但是当有文件名或其他参数时,这不起作用。任何帮助不胜感激。
这将执行您想要的操作:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; // the URL you want to inject the parameters into
$params = "new=yes!"; // the new parameters you want to add at the beginning
if (strpos($url, "?") !== false) {
list($url, $b) = explode("?", $url, 2);
$params = "$params&$b";
}
$url .= "?".$params;
输出:http://example.com/example.php?new=yes!&a=b&c=no
如果要将某个参数放在查询字符串的开头,可以使用parse_url
函数:
$url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$parsed_url = parse_url($url);
$new_url = $parsed_url['path'] ."?foo=bar" . ((isset($parsed_url['query']))? urlencode("&").$parsed_url['query'] : "");
var_dump($new_url);
// the output: string(45) "http://www.domain.com?foo=bar%26param=value"