How to get and change URL variable PHP

前端 未结 7 1877
梦谈多话
梦谈多话 2020-12-29 23:24

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

7条回答
  •  情话喂你
    2020-12-29 23:57

    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- 'test3']?> then you will get -

提交回复
热议问题