Hi could anyone help me with this I have a URL like
parent/child/a=1&b=2$c=3
then I have a link that would add variable to that URL
function replaceUrlParameters($url = '', $newParams = array()){
if($url){
$urlArray = parse_url($url);
$queryString = $urlArray['query'];
parse_str($queryString, $queryParams);
$queryParams = array_merge($queryParams, $newParams);
$urlArray['query'] = http_build_query($queryParams);
if(!empty($urlArray)){
$url = $urlArray['scheme'].'://'.$urlArray['host'].$urlArray['path'].'?'.$urlArray['query'];
}
}
return $url;
}
// $newParams = array of new parameters or old parameters with new value
$replacedUrl = replaceUrlParameters($url, $newParams);
Suppose you have url like :- and you want to replace parameter b's value to test3 then just pass this url and an array with the same index with new value. for ex- $url = $_SERVER["REQUEST_URI"]."&a=test1&b=test2";?> $newParams = ['b' => 'test3']?> then you will get - $_SERVER["REQUEST_URI"]."&a=test1&b=test3";?>