Beautiful way to remove GET variables using PHP?

I have a full URL string including GET variables. What is the best way to remove GET variables? Is there a good way to remove only one of them?

This is code that works, but not very pretty (I think):

$current_url = explode('?', $current_url); echo $current_url[0]; 

The above code just removes all the GET variables. The URL in my case was created from CMS, so I do not need information about server variables.

+73
variables url php get
Aug 09 '09 at 15:30
source share
11 answers

Well, to remove all the variables, perhaps the most beautiful -

 $url = strtok($url, '?'); 

See strtok here .

The fastest (see below), and processes URLs without '?' properly.

To take url + querystring and remove only one variable (without using a regex replacement, which might be faster in some cases), you can do something like:

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

Replacing a regex to remove one var might look like this:

 function removeqsvar($url, $varname) { return preg_replace('/([?&])'.$varname.'=[^&]+(&|$)/','$1',$url); } 

Sets the timings of several different methods, providing reset synchronization between loops.

 <?php $number_of_tests = 40000; $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; preg_replace('/\\?.*/', '', $str); } $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "regexp execution time: ".$totaltime." seconds; "; $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; $str = explode('?', $str); } $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "explode execution time: ".$totaltime." seconds; "; $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; $qPos = strpos($str, "?"); $url_without_query_string = substr($str, 0, $qPos); } $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "strpos execution time: ".$totaltime." seconds; "; $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; $url_without_query_string = strtok($str, '?'); } $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "tok execution time: ".$totaltime." seconds; "; 

shows

 regexp execution time: 0.14604902267456 seconds; explode execution time: 0.068033933639526 seconds; strpos execution time: 0.064775943756104 seconds; tok execution time: 0.045819044113159 seconds; regexp execution time: 0.1408839225769 seconds; explode execution time: 0.06751012802124 seconds; strpos execution time: 0.064877986907959 seconds; tok execution time: 0.047760963439941 seconds; regexp execution time: 0.14162802696228 seconds; explode execution time: 0.065848112106323 seconds; strpos execution time: 0.064821004867554 seconds; tok execution time: 0.041788101196289 seconds; regexp execution time: 0.14043688774109 seconds; explode execution time: 0.066350221633911 seconds; strpos execution time: 0.066242933273315 seconds; tok execution time: 0.041517972946167 seconds; regexp execution time: 0.14228296279907 seconds; explode execution time: 0.06665301322937 seconds; strpos execution time: 0.063700199127197 seconds; tok execution time: 0.041836977005005 seconds; 

strtok wins and is by far the smallest code.

+196
Aug 09 '09 at 16:05
source share

What about:

 preg_replace('/\\?.*/', '', $str) 
+26
Aug 09 '09 at 15:37
source share

Inspired by a comment by @MitMaro, I wrote a small guideline to check the speed of solutions @Gumbo, @Matt Bridges and @justin suggestion in question:

 function teststrtok($number_of_tests){ for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; $str = strtok($str,'?'); } } function testexplode($number_of_tests){ for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; $str = explode('?', $str); } } function testregexp($number_of_tests){ for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; preg_replace('/\\?.*/', '', $str); } } function teststrpos($number_of_tests){ for($i = 0; $i < $number_of_tests; $i++){ $str = "http://www.example.com?test=test"; $qPos = strpos($str, "?"); $url_without_query_string = substr($str, 0, $qPos); } } $number_of_runs = 10; for($runs = 0; $runs < $number_of_runs; $runs++){ $number_of_tests = 40000; $functions = array("strtok", "explode", "regexp", "strpos"); foreach($functions as $func){ $starttime = microtime(true); call_user_func("test".$func, $number_of_tests); echo $func.": ". sprintf("%0.2f",microtime(true) - $starttime).";"; } echo "<br />"; } 
 strtok: 0.12; explode: 0.19; regexp: 0.31; strpos: 0.18;
 strtok: 0.12; explode: 0.19; regexp: 0.31; strpos: 0.18;
 strtok: 0.12; explode: 0.19; regexp: 0.31; strpos: 0.18;
 strtok: 0.12; explode: 0.19; regexp: 0.31; strpos: 0.18;
 strtok: 0.12; explode: 0.19; regexp: 0.31; strpos: 0.18;
 strtok: 0.12; explode: 0.19; regexp: 0.31; strpos: 0.18;
 strtok: 0.12; explode: 0.19; regexp: 0.31; strpos: 0.18;
 strtok: 0.12; explode: 0.19; regexp: 0.31; strpos: 0.18;
 strtok: 0.12; explode: 0.19; regexp: 0.31; strpos: 0.18;
 strtok: 0.12; explode: 0.19; regexp: 0.31; strpos: 0.18;

Result: @justin strtok is the fastest.

Note: tested on local Debian Lenny system with Apache2 and PHP5.

+8
Aug 09 '09 at 16:00
source share

If the URL you are trying to remove the query string is the current URL of the PHP script, you can use one of the previously mentioned methods. If you just have a string variable with a url in it and you want to delete everything due to "?" You can do:

 $pos = strpos($url, "?"); $url = substr($url, 0, $pos); 
+7
Aug 09 '09 at 15:40
source share

Another solution ... I find this feature more elegant, it will also remove the final '?' if the key to delete is the only one in the query string.

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

Tests:

 $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. '<br/>'; echo removeQueryStringParameter($url, 'test'). '<br/><br/>'; } 

It will display:

 http://www.example.com?test=test http://www.example.com http://www.example.com?bar=foo&test=test2&foo2=dooh http://www.example.com?bar=foo&foo2=dooh http://www.example.com http://www.example.com http://www.example.com?foo=bar http://www.example.com?foo=bar http://www.example.com/test/no-empty-path/?foo=bar&test=test5 http://www.example.com/test/no-empty-path/?foo=bar https://www.example.com/test/test.test?test=test6 https://www.example.com/test/test.test 

& raquo; Run these tests on 3v4l

+5
Jul 03 '15 at 13:41
source share

You can use server variables for this, for example $_SERVER['REQUEST_URI'] or even better: $_SERVER['PHP_SELF'] .

+2
Aug 09 '09 at 15:33
source share

Could you use server variables for this?

Or does it work ?:

 unset($_GET['page']); $url = $_SERVER['SCRIPT_NAME'] ."?".http_build_query($_GET); 

Just a thought.

+2
Apr 25 '13 at 12:38
source share

How about a function to rewrite the query string by going through the $ _GET array

! Rough outline of a suitable function

 function query_string_exclude($exclude, $subject = $_GET, $array_prefix=''){ $query_params = array; foreach($subject as $key=>$var){ if(!in_array($key,$exclude)){ if(is_array($var)){ //recursive call into sub array $query_params[] = query_string_exclude($exclude, $var, $array_prefix.'['.$key.']'); }else{ $query_params[] = (!empty($array_prefix)?$array_prefix.'['.$key.']':$key).'='.$var; } } } return implode('&',$query_params); } 

Something like this would be nice to keep on hand for links to pages, etc.

 <a href="?p=3&<?= query_string_exclude(array('p')) ?>" title="Click for page 3">Page 3</a> 
0
Aug 09 '09 at 15:51
source share
 @list($url) = explode("?", $url, 2); 
0
Aug 02 2018-10-12T00:
source share

basename($_SERVER['REQUEST_URI']) returns everything after and including "?",

In my code, sometimes I only need sections, so separate it so that I can get the value of what I need on the fly. Not sure about speed compared to other methods, but it is really useful for me.

 $urlprotocol = 'http'; if ($_SERVER["HTTPS"] == "on") {$urlprotocol .= "s";} $urlprotocol .= "://"; $urldomain = $_SERVER["SERVER_NAME"]; $urluri = $_SERVER['REQUEST_URI']; $urlvars = basename($urluri); $urlpath = str_replace($urlvars,"",$urluri); $urlfull = $urlprotocol . $urldomain . $urlpath . $urlvars; 
0
Apr 04 '14 at 17:08
source share

In my opinion, the best way would be the following:

 <? if(isset($_GET['i'])){unset($_GET['i']); header('location:/');} ?> 

It checks to see if there is a "i" GET parameter and removes it, if any.

0
Feb 01 '17 at 2:13
source share



All Articles