How to get and change URL variable PHP

前端 未结 7 1719
梦谈多话
梦谈多话 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:50

    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;
    
    }
    

提交回复
热议问题