public
Created

add_querystring_var() and remove_querystring_var() improvements, with tests

  • Download Gist
QueryStringTest.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
<?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.');
}
 
}
querystring.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
<?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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.