<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:series="http://unfoldingneurons.com/"
> <channel><title>Comments on: PHP Function:  Remove a Query String&#160;Key=&gt;Value</title> <atom:link href="http://davidwalsh.name/php-remove-variable/feed" rel="self" type="application/rss+xml" /><link>http://davidwalsh.name/php-remove-variable</link> <description>Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</description> <lastBuildDate>Thu, 09 Feb 2012 09:28:55 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3</generator> <item><title>By: Krrish</title><link>http://davidwalsh.name/php-remove-variable/comment-page-1#comment-26870</link> <dc:creator>Krrish</dc:creator> <pubDate>Mon, 12 Sep 2011 11:07:31 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=4626#comment-26870</guid> <description>RegEx are Tough to understand... But they solve our problems in short...
I Use array in such Tasks.</description> <content:encoded><![CDATA[<p>RegEx are Tough to understand&#8230; But they solve our problems in short&#8230;<br
/> I Use array in such Tasks.</p> ]]></content:encoded> </item> <item><title>By: Tony</title><link>http://davidwalsh.name/php-remove-variable/comment-page-1#comment-26239</link> <dc:creator>Tony</dc:creator> <pubDate>Tue, 26 Jul 2011 15:40:49 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=4626#comment-26239</guid> <description>I took the regex and expanded it.  PHP allows array references in the querystring vars such as http://www.example.com/?a[]=1&amp;a[0]=3&amp;a[5][7][89][fdds][ddd]=44Also, this function allows the removal of multiple vars.&lt;code&gt;
echo htmlentities(getRedirectURL(&quot;http://www.example.com/?a[]=1&amp;a[0]=3&amp;a[5][7][89][fdds][ddd]=44&quot;,&quot;a&quot;));
echo htmlentities(getRedirectURL(&quot;http://www.example.com/?a=1&amp;a=2&amp;b=3&amp;c=4&quot;,array(&quot;a&quot;,&quot;c&quot;)));
function getRedirectURL($url,$keysToRemove) {
if (!is_array($keysToRemove))
$keysToRemove = array($keysToRemove);
foreach ($keysToRemove as $key)
$url = preg_replace(&#039;/(?:&amp;&#124;(\\?&#124;\\G))&#039; . urlencode($key) . &#039;(?:\\[[^]]*\\])*=[^&amp;]*(?(1)&amp;&#124;)?/i&#039;, &quot;$1&quot;, $url);
if (substr($url,-1) == &#039;?&#039;)
$url = substr($url,0,-1);
return $url;
}
&lt;/code&gt;</description> <content:encoded><![CDATA[<p>I took the regex and expanded it.  PHP allows array references in the querystring vars such as <a
href="http://www.example.com/?a=1&#038;a0=3&#038;a5789fddsddd=44" rel="nofollow">http://www.example.com/?a=1&#038;a0=3&#038;a5789fddsddd=44</a></p><p>Also, this function allows the removal of multiple vars.</p><p><code><br
/> echo htmlentities(getRedirectURL("http://www.example.com/?a[]=1&amp;a[0]=3&amp;a[5][7][89][fdds][ddd]=44","a"));<br
/> echo htmlentities(getRedirectURL("http://www.example.com/?a=1&amp;a=2&amp;b=3&amp;c=4",array("a","c")));<br
/> function getRedirectURL($url,$keysToRemove) {<br
/> if (!is_array($keysToRemove))<br
/> $keysToRemove = array($keysToRemove);</p><p> foreach ($keysToRemove as $key)<br
/> $url = preg_replace('/(?:&amp;|(\\?|\\G))' . urlencode($key) . '(?:\\[[^]]*\\])*=[^&amp;]*(?(1)&amp;|)?/i', "$1", $url);</p><p> if (substr($url,-1) == '?')<br
/> $url = substr($url,0,-1);</p><p> return $url;<br
/> }<br
/> </code></p> ]]></content:encoded> </item> <item><title>By: Adrian Callaghan</title><link>http://davidwalsh.name/php-remove-variable/comment-page-1#comment-23477</link> <dc:creator>Adrian Callaghan</dc:creator> <pubDate>Wed, 13 Apr 2011 09:58:57 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=4626#comment-23477</guid> <description>thanks!! lots of cool ways.I always like a regular expression but an alternative would be to use the ? as a delimiter and explode the uri into an array and retrieve the first result i.eecho reset(explode(&#039;?&#039;,$_SERVER[&#039;REQUEST_URI&#039;]));</description> <content:encoded><![CDATA[<p>thanks!! lots of cool ways.</p><p>I always like a regular expression but an alternative would be to use the ? as a delimiter and explode the uri into an array and retrieve the first result i.e</p><p>echo reset(explode(&#8216;?&#8217;,$_SERVER['REQUEST_URI']));</p> ]]></content:encoded> </item> <item><title>By: Charlie</title><link>http://davidwalsh.name/php-remove-variable/comment-page-1#comment-18463</link> <dc:creator>Charlie</dc:creator> <pubDate>Mon, 28 Jun 2010 19:17:25 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=4626#comment-18463</guid> <description>I had an array of checkbox values being passed in the querystring, and I don&#039;t think these functions work for removing it.I found it easier to just directly define the $_SERVER[&#039;QUERY_STRING&#039;] by concatenating the $_GET[] variables I wanted to pass, and leaving out the ones I didn&#039;t (the checkbox array)</description> <content:encoded><![CDATA[<p>I had an array of checkbox values being passed in the querystring, and I don&#8217;t think these functions work for removing it.</p><p>I found it easier to just directly define the $_SERVER['QUERY_STRING'] by concatenating the $_GET[] variables I wanted to pass, and leaving out the ones I didn&#8217;t (the checkbox array)</p> ]]></content:encoded> </item> <item><title>By: Charlie</title><link>http://davidwalsh.name/php-remove-variable/comment-page-1#comment-18462</link> <dc:creator>Charlie</dc:creator> <pubDate>Mon, 28 Jun 2010 19:16:54 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=4626#comment-18462</guid> <description>I had an array of checkbox values being passed in the querystring, and I don&#039;t think these functions work for removing it.I found it easier to just directly define the $_SERVER[&#039;QUERY_STRING&#039;] by concatenating the $_GET[] variables I wanted to pass, and leaving out the ones I didn&#039;t (the checkbox array)</description> <content:encoded><![CDATA[<p>I had an array of checkbox values being passed in the querystring, and I don&#8217;t think these functions work for removing it.</p><p>I found it easier to just directly define the $_SERVER['QUERY_STRING'] by concatenating the $_GET[] variables I wanted to pass, and leaving out the ones I didn&#8217;t (the checkbox array)</p> ]]></content:encoded> </item> <item><title>By: Adam</title><link>http://davidwalsh.name/php-remove-variable/comment-page-1#comment-17418</link> <dc:creator>Adam</dc:creator> <pubDate>Wed, 05 May 2010 14:05:56 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=4626#comment-17418</guid> <description>@Adam:  Oh and just for making it even more condensed, shorten the whole function to this:function remove_qs_key($url, $key) {
return preg_replace(&#039;/(?:&amp;&#124;(\?))&#039; . $key . &#039;=[^&amp;]*(?(1)&amp;&#124;)?/i&#039;, &quot;$1&quot;, $url);
}</description> <content:encoded><![CDATA[<p>@Adam:  Oh and just for making it even more condensed, shorten the whole function to this:</p><p>function remove_qs_key($url, $key) {<br
/> return preg_replace(&#8216;/(?:&amp;|(\?))&#8217; . $key . &#8216;=[^&amp;]*(?(1)&amp;|)?/i&#8217;, &#8220;$1&#8243;, $url);<br
/> }</p> ]]></content:encoded> </item> <item><title>By: Adam</title><link>http://davidwalsh.name/php-remove-variable/comment-page-1#comment-17417</link> <dc:creator>Adam</dc:creator> <pubDate>Wed, 05 May 2010 14:04:03 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=4626#comment-17417</guid> <description>@Dustin Hansen:  Dustin&#039;s solution is the most elegant and I found the other solutions to have issues with parameter&#039;s at the beginning and end of the query string - in my testing Dustin&#039;s solution worked for parameters at all positions.</description> <content:encoded><![CDATA[<p>@Dustin Hansen:  Dustin&#8217;s solution is the most elegant and I found the other solutions to have issues with parameter&#8217;s at the beginning and end of the query string &#8211; in my testing Dustin&#8217;s solution worked for parameters at all positions.</p> ]]></content:encoded> </item> <item><title>By: Tom Spizuoco</title><link>http://davidwalsh.name/php-remove-variable/comment-page-1#comment-16286</link> <dc:creator>Tom Spizuoco</dc:creator> <pubDate>Sun, 28 Feb 2010 19:57:36 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=4626#comment-16286</guid> <description>I like the parse_str/http_query_build idea so I extended it to include the ability to remove an array of values and add an array of values.function querystring($strQS, $arRemove, $arAdd = NULL)
{
parse_str($strQS, $arQS);
$arQS = array_diff_key($arQS, array_flip($arRemove));
$arQS = $arQS + $arAdd;
return http_build_query($arQS);
}Example:
Current URL = http://www.example.com?one=1&amp;two=2&amp;three=3&amp;four=4$arR = array(&#039;two&#039;, &#039;four&#039;);
$arA = array(&#039;five&#039; =&gt; 5, &#039;six&#039; =&gt; 6);
echo querystring($_SERVER[&#039;QUERY_STRING&#039;], $arR, $arA);prints &quot;one=1&amp;three=3&amp;five=5&amp;six=6&quot;</description> <content:encoded><![CDATA[<p>I like the parse_str/http_query_build idea so I extended it to include the ability to remove an array of values and add an array of values.</p><p>function querystring($strQS, $arRemove, $arAdd = NULL)<br
/> {<br
/> parse_str($strQS, $arQS);<br
/> $arQS = array_diff_key($arQS, array_flip($arRemove));<br
/> $arQS = $arQS + $arAdd;<br
/> return http_build_query($arQS);<br
/> }</p><p>Example:<br
/> Current URL = <a
href="http://www.example.com?one=1&#038;two=2&#038;three=3&#038;four=4" rel="nofollow">http://www.example.com?one=1&#038;two=2&#038;three=3&#038;four=4</a></p><p>$arR = array(&#8216;two&#8217;, &#8216;four&#8217;);<br
/> $arA = array(&#8216;five&#8217; =&gt; 5, &#8216;six&#8217; =&gt; 6);<br
/> echo querystring($_SERVER['QUERY_STRING'], $arR, $arA);</p><p>prints &#8220;one=1&amp;three=3&amp;five=5&amp;six=6&#8243;</p> ]]></content:encoded> </item> <item><title>By: Dustin Hansen</title><link>http://davidwalsh.name/php-remove-variable/comment-page-1#comment-16126</link> <dc:creator>Dustin Hansen</dc:creator> <pubDate>Tue, 16 Feb 2010 01:40:15 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=4626#comment-16126</guid> <description>Still that entire regex could be reduced to:preg_replace(&#039;/(?:&amp;&#124;(\?))&#039; . $key . &#039;=[^&amp;]*(?(1)&amp;&#124;)?/i&#039;, &quot;$1&quot;, $url);</description> <content:encoded><![CDATA[<p>Still that entire regex could be reduced to:</p><p>preg_replace(&#8216;/(?:&amp;|(\?))&#8217; . $key . &#8216;=[^&amp;]*(?(1)&amp;|)?/i&#8217;, &#8220;$1&#8243;, $url);</p> ]]></content:encoded> </item> <item><title>By: Dimitri Uwarov</title><link>http://davidwalsh.name/php-remove-variable/comment-page-1#comment-16120</link> <dc:creator>Dimitri Uwarov</dc:creator> <pubDate>Mon, 15 Feb 2010 20:40:12 +0000</pubDate> <guid
isPermaLink="false">http://davidwalsh.name/?p=4626#comment-16120</guid> <description>Hi,nice code, but it did not work for the first parameter, because the &quot;?&quot; should be escaped: &quot;\?&quot; to match.
See here: http://php.net/manual/de/function.preg-replace.phpif i add escape it works for me:function remove_querystring_var($url, $key) {
$url = preg_replace(&#039;/(.*)(\?&#124;&amp;)&#039; . $key . &#039;=[^&amp;]+?(&amp;)(.*)/i&#039;, &#039;$1$2$4&#039;, $url . &#039;&amp;&#039;);
$url = substr($url, 0, -1);
return $url;
}echo remove_querystring_var(&quot;http://www.thinkplexx.com/?errors=%0A%09The+password+field+is+empty.%3B&quot;,&quot;errors&quot;);</description> <content:encoded><![CDATA[<p>Hi,</p><p>nice code, but it did not work for the first parameter, because the &#8220;?&#8221; should be escaped: &#8220;\?&#8221; to match.<br
/> See here: <a
href="http://php.net/manual/de/function.preg-replace.php" rel="nofollow">http://php.net/manual/de/function.preg-replace.php</a></p><p>if i add escape it works for me:</p><p>function remove_querystring_var($url, $key) {<br
/> $url = preg_replace(&#8216;/(.*)(\?|&amp;)&#8217; . $key . &#8216;=[^&amp;]+?(&amp;)(.*)/i&#8217;, &#8216;$1$2$4&#8242;, $url . &#8216;&amp;&#8217;);<br
/> $url = substr($url, 0, -1);<br
/> return $url;<br
/> }</p><p>echo remove_querystring_var(&#8220;http://www.thinkplexx.com/?errors=%0A%09The+password+field+is+empty.%3B&#8221;,&#8221;errors&#8221;);</p> ]]></content:encoded> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching using disk: basic
Object Caching 865/866 objects using disk: basic

Served from: davidwalsh.name @ 2012-02-09 04:37:22 -->
