Manipulate a url string by adding GET parameters

前端 未结 15 2151
旧巷少年郎
旧巷少年郎 2020-11-29 00:50

I want to add GET parameters to URLs that may and may not contain GET parameters without repeating ? or &.

Example:

If I want t

15条回答
  •  感动是毒
    2020-11-29 01:13

     public function addGetParamToUrl($url, $params)
    {
        foreach ($params as $param) {
             if (strpos($url, "?"))
            {
                $url .= "&" .http_build_query($param); 
            }
            else
            {
                $url .= "?" .http_build_query($param); 
            }
        }
        return $url;
    }
    

提交回复
热议问题