GuzzleHttp\Psr7\Uri::withoutQueryValue PHP Method

withoutQueryValue() public static method

Any existing query string values that exactly match the provided key are removed.
public static withoutQueryValue ( Psr\Http\Message\UriInterface $uri, string $key ) : Psr\Http\Message\UriInterface
$uri Psr\Http\Message\UriInterface URI to use as a base.
$key string Query string key to remove.
return Psr\Http\Message\UriInterface
    public static function withoutQueryValue(UriInterface $uri, $key)
    {
        $current = $uri->getQuery();
        if ($current === '') {
            return $uri;
        }
        $decodedKey = rawurldecode($key);
        $result = array_filter(explode('&', $current), function ($part) use($decodedKey) {
            return rawurldecode(explode('=', $part)[0]) !== $decodedKey;
        });
        return $uri->withQuery(implode('&', $result));
    }

Usage Example

Example #1
0
 public function testAddAndRemoveQueryValues()
 {
     $uri = new Uri('http://foo.com/bar');
     $uri = Uri::withQueryValue($uri, 'a', 'b');
     $uri = Uri::withQueryValue($uri, 'c', 'd');
     $uri = Uri::withQueryValue($uri, 'e', null);
     $this->assertEquals('a=b&c=d&e', $uri->getQuery());
     $uri = Uri::withoutQueryValue($uri, 'c');
     $uri = Uri::withoutQueryValue($uri, 'e');
     $this->assertEquals('a=b', $uri->getQuery());
     $uri = Uri::withoutQueryValue($uri, 'a');
     $uri = Uri::withoutQueryValue($uri, 'a');
     $this->assertEquals('', $uri->getQuery());
 }
All Usage Examples Of GuzzleHttp\Psr7\Uri::withoutQueryValue