PHP: How to remove specific query string from a url?

There are many ways via which this can be achieved, some of the tested code is written below:

The following function will remove only the query string you want to remove:

function removeqsvar($url, $varname) {
    list($urlpart, $qspart) = array_pad(explode('?', $url), 2, '');
    parse_str($qspart, $qsvars);
    unset($qsvars[$varname]);
    $newqs = http_build_query($qsvars); return $urlpart . '?' . $newqs; 
}

Example Use:

echo removeqsvar('http://google.com/query-test/?date=20140725&category=art-literature','date');

If you have a better code to achieve above, please leave it as a comment so we all experiment together : )

You may also like...