Skip to content

Instantly share code, notes, and snippets.

@philfreo
Created June 22, 2011 19:44
Show Gist options
  • Save philfreo/1040968 to your computer and use it in GitHub Desktop.
Save philfreo/1040968 to your computer and use it in GitHub Desktop.
add_querystring_var() and remove_querystring_var() improvements, with tests
<?php
/**
* Add the querystring variable $key with a value $value to $url.
* If $key is already specified within $url, it will replace it.
*
* Based upon http://www.addedbytes.com/code/querystring-functions/
*
* @param string $url
* @param string $key
* @param string[optional] $value
* @return string
*/
function add_querystring_var($url, $key, $value = '') {
$url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
$url = substr($url, 0, -1);
$value = $value ? "=".urlencode($value) : '';
if (strpos($url, '?') === false)
return ($url . '?' . $key . $value);
else
return ($url . '&' . $key . $value);
}
/**
* Remove the Querystring variable $key and its value from the given $url.
* @param string $url
* @param string $key
* @return string
*/
function remove_querystring_var($url, $key) {
$parts = parse_url($url);
$qs = isset($parts['query']) ? $parts['query'] : '';
$base = $qs ? mb_substr($url, 0, mb_strpos($url, '?')) : $url; // all of URL except QS
parse_str($qs, $qsParts);
unset($qsParts[$key]);
$newQs = rtrim(http_build_query($qsParts), '=');
if ($newQs)
return $base.'?'.$newQs;
else
return $base;
}
<?php
class QueryStringTest extends PHPUnit_Framework_TestCase {
/**
* @covers remove_querystring_var
*/
public function testRemoveQueryStringVar() {
// tests without any other querystring params
$urls = array(
'http://example.com/faqs/foo-bar/',
'http://example.com/faqs/foo-bar/?removeme',
'http://example.com/faqs/foo-bar/?removeme=',
'http://example.com/faqs/foo-bar/?removeme&',
'http://example.com/faqs/foo-bar/?removeme=en',
'http://example.com/faqs/foo-bar/?removeme=en&',
);
$expected = $urls[0];
foreach($urls as $k => $url)
$this->assertEquals($expected, remove_querystring_var($url, 'removeme'), 'First batch: $urls['.$k.'] has a problem.');
// tests with other querystring params
$urls = array(
'http://example.com/faqs/foo-bar/?foo',
'http://example.com/faqs/foo-bar/?removeme=en&foo',
'http://example.com/faqs/foo-bar/?removeme=en&foo&another=foo',
'http://example.com/faqs/foo-bar/?foo&removeme&',
'http://example.com/faqs/foo-bar/?foo&removeme',
'http://example.com/faqs/foo-bar/?foo&removeme=',
'http://example.com/faqs/foo-bar/?foo&removeme=&',
'http://example.com/faqs/foo-bar/?foo&removeme=en',
'http://example.com/faqs/foo-bar/?foo&removeme=en&',
'http://example.com/faqs/foo-bar/?foo&removeme=en&another=foo',
);
$expected = $urls[0];
foreach($urls as $k => $url)
$this->assertEquals($expected, remove_querystring_var(remove_querystring_var($url, 'removeme'), 'another'), 'Second batch: $urls['.$k.'] has a problem.');
// some tests without a full URL without any other querystring params
$urls = array(
'/faqs/foo-bar/',
'/faqs/foo-bar/?removeme',
'/faqs/foo-bar/?removeme=',
'/faqs/foo-bar/?removeme&',
'/faqs/foo-bar/?removeme=en',
'/faqs/foo-bar/?removeme=en&',
'/faqs/foo-bar/?removeme=en&another',
'/faqs/foo-bar/?removeme=en&another=',
'/faqs/foo-bar/?removeme=en&another=foo',
'/faqs/foo-bar/?another&removeme',
'/faqs/foo-bar/?another&removeme=',
'/faqs/foo-bar/?another=hi&removeme&',
'/faqs/foo-bar/?another&removeme=en',
'/faqs/foo-bar/?another&removeme=en&',
);
$expected = $urls[0];
foreach($urls as $k => $url)
$this->assertEquals($expected, remove_querystring_var(remove_querystring_var($url, 'removeme'), 'another'), 'Third batch: $urls['.$k.'] has a problem.');
// some tests without a full URL with other querystring params
$urls = array(
'/faqs/foo-bar/?foo=baz',
'/faqs/foo-bar/?removeme=en&foo=baz',
'/faqs/foo-bar/?removeme=en&foo=baz&another=foo',
'/faqs/foo-bar/?foo=baz&removeme&',
'/faqs/foo-bar/?foo=baz&removeme',
'/faqs/foo-bar/?foo=baz&removeme=',
'/faqs/foo-bar/?foo=baz&removeme=&',
'/faqs/foo-bar/?foo=baz&removeme=en',
'/faqs/foo-bar/?foo=baz&removeme=en&',
'/faqs/foo-bar/?foo=baz&removeme=en&another=foo',
);
$expected = $urls[0];
foreach($urls as $k => $url)
$this->assertEquals($expected, remove_querystring_var(remove_querystring_var($url, 'removeme'), 'another'), 'Fourth batch: $urls['.$k.'] has a problem.');
}
}
@kudataz
Copy link

kudataz commented Jan 29, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment