How to get and change URL variable PHP
Try with the below expression, It Should Work
preg_replace("#&d=.*&#", '&d=newvalue&', $_SERVER['REQUEST_URI'])
To remove repeated addition of query parameter do the below
// parse the url
$pathInfo = parse_url($_SERVER['REQUEST_URI']);
$queryString = $pathInfo['query'];
// convert the query parameters to an array
parse_str($queryString, $queryArray);
// add the new query parameter into the array
$queryArray['d'] = 1;
// build the new query string
$newQueryStr = http_build_query($queryArray);
// construct new url
?>
<a href="<?php echo $pathInfo['host'].'?'.$newQueryStr;?>">LINK 1</a>
$query = $_GET;
// replace parameter(s)
$query['d'] = 'new_value';
// rebuild url
$query_result = http_build_query($query);
// new link
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_result; ?>">Link</a>
modify_url_query($url, array('limit' => 50));
My function for modifying query in url
function modify_url_query($url, $mod){
$purl = parse_url($url);
$params = array();
if (($query_str=$purl['query']))
{
parse_str($query_str, $params);
foreach($params as $name => $value)
{
if (isset($mod[$name]))
{
$params[$name] = $mod[$name];
unset($mod[$name]);
}
}
}
$params = array_merge($params, $mod);
$ret = "";
if ($purl['scheme'])
{
$ret = $purl['scheme'] . "://";
}
if ($purl['host'])
{
$ret .= $purl['host'];
}
if ($purl['path'])
{
$ret .= $purl['path'];
}
if ($params)
{
$ret .= '?' . http_build_query($params);
}
if ($purl['fragment'])
{
$ret .= "#" . $purl['fragment'];
}
return $ret;
}