Piękny sposób na usunięcie GET-zmiennych za pomocą PHP?

Mam ciąg z pełnym adresem URL zawierającym zmienne GET. Jaki jest najlepszy sposób na usunięcie zmiennych GET? Czy istnieje dobry sposób, aby usunąć tylko jeden z nich?

To jest kod, który działa, ale nie jest zbyt piękny (myślę):

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

Powyższy kod usuwa wszystkie zmienne GET. Adres URL jest w moim przypadku wygenerowany z CMS więc nie potrzebuję żadnych informacji o zmiennych serwera.

Author: Jens Törnell, 2009-08-09

12 answers

Ok, aby usunąć wszystkie zmienne, może najładniejsza jest

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

Zobacz o strtok tutaj .

Jest najszybszy (patrz poniżej) i obsługuje adresy URL bez'?'odpowiednio.

Aby pobrać url+querystring i usunąć tylko jedną zmienną (bez użycia zastępowania regex, co może być szybsze w niektórych przypadkach), możesz zrobić coś w stylu:

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

Regex replace do usunięcia pojedynczego var może wyglądać następująco:

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

Oto czasy kilku różnych metod, upewnienie się, że czas jest zresetowany między uruchomieniami.

<?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; ";

Pokazuje

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 wygrywa i jest zdecydowanie najmniejszym kodem.

 241
Author: Justin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-12 03:24:26

A może:

preg_replace('/\\?.*/', '', $str)
 33
Author: Gumbo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-09 15:37:25

Jeśli adres URL, z którego próbujesz usunąć ciąg zapytania, jest bieżącym adresem URL skryptu PHP, możesz użyć jednej z wcześniej wymienionych metod. Jeśli po prostu masz zmienną string z adresem URL w nim i chcesz rozebrać wszystko poza '?"you can do:

$pos = strpos($url, "?");
$url = substr($url, 0, $pos);
 10
Author: Matt Bridges,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-09 15:52:46

Zainspirowany komentarzem @ MitMaro, napisałem mały benchmark, aby przetestować szybkość rozwiązań @ Gumbo, @ Matt Bridges i @justin propozycja w pytaniu:

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;

Wynik :strtok @ justin jest najszybszy.

Uwaga: testowane na lokalnym systemie Debian Lenny z Apache2 i PHP5.

 7
Author: Scharrels,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-09 17:05:39

Inne rozwiązanie... Uważam, że ta funkcja jest bardziej elegancka, usunie również końcowe '?'jeśli klucz do usunięcia jest jedynym w łańcuchu zapytania.

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

Testy:

$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/>';
}

Wyświetli:

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

" Uruchom te testy na 3v4l

 7
Author: COil,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-22 11:13:03

Nie możesz użyć zmiennych serwera do tego celu?

Czy to zadziała?:
unset($_GET['page']);
$url = $_SERVER['SCRIPT_NAME'] ."?".http_build_query($_GET);
Tak sobie pomyślałem.
 4
Author: bobert,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-13 10:47:48

Możesz użyć do tego zmiennych serwera , na przykład $_SERVER['REQUEST_URI'], lub jeszcze lepiej: $_SERVER['PHP_SELF'].

 2
Author: Scharrels,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-09 15:33:29
@list($url) = explode("?", $url, 2);
 2
Author: Rob Haswell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-02 12:43:11

Jak o funkcji do przepisania łańcucha zapytania przez zapętlenie przez tablicę $_GET

! Przybliżony zarys odpowiedniej funkcji

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

Coś takiego dobrze by było trzymać pod ręką linki do paginacji itp.

<a href="?p=3&<?= query_string_exclude(array('p')) ?>" title="Click for page 3">Page 3</a>
 0
Author: Question Mark,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-09 15:51:24

basename($_SERVER['REQUEST_URI']) zwraca wszystko po I włącznie z"?',

W moim kodzie czasami potrzebuję tylko sekcji, więc oddziel go, abym mógł uzyskać wartość tego, czego potrzebuję w locie. Nie jestem pewien szybkości działania w porównaniu do innych metod, ale jest to naprawdę przydatne dla mnie.

$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
Author: Sidupac,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-04 17:25:43

Moim zdaniem najlepszym sposobem byłoby to:

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

Sprawdza, czy istnieje parametr " I " GET i usuwa go, jeśli istnieje.

 0
Author: Joshua Anderson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-01 02:13:30

Po prostu użyj echo ' d javascript, aby pozbyć się adresu URL dowolnych zmiennych za pomocą samo-wysyłającego, pustego formularza:

    <?
    if (isset($_GET['your_var'])){
    //blah blah blah code
    echo "<script type='text/javascript'>unsetter();</script>"; 
    ?> 

Następnie wykonaj tę funkcję javascript:

    function unsetter() {
    $('<form id = "unset" name = "unset" METHOD="GET"><input type="submit"></form>').appendTo('body');
    $( "#unset" ).submit();
    }
 0
Author: PlanB,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-04-22 11:10:04