3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Remove a query string parameter from an URL. * * @param string $url * @param string $varname * * @return string */ function removeQueryStringParameter($url, $varname) { $parsedUrl = parse_url($url); $query = array(); if (isset($parsedUrl['query'])) { parse_str($parsedUrl['query'], $query); unset($query[$varname]); } $path = isset($parsedUrl['path']) ? $parsedUrl['path'] : ''; $query = !empty($query) ? '?'. http_build_query($query) : ''; return $parsedUrl['scheme']. '://'. $parsedUrl['host']. $path. $query; } $urls = array( 'http://www.example.com?test=test', 'http://www.example.com?bar=foo&test=test2&foo2=dooh', 'http://www.example.com', 'http://www.example.com?foo=bar', 'http://www.example.com/test/no-empty-path/?foo=bar&test=test5', 'https://www.example.com/test/test.test?test=test6', ); foreach ($urls as $url) { echo $url.PHP_EOL; echo removeQueryStringParameter($url, 'test'). PHP_EOL. PHP_EOL; }

preferences:
37.4 ms | 406 KiB | 5 Q