note 108358 modified in reserved.variables.get by sp

From: Date: Sat, 21 Apr 2012 10:30:19 +0000
Subject: note 108358 modified in reserved.variables.get by sp
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187272@lists.php.net to get a copy of this message
A way of removing keys from $_GET - in my opinion - in a more elegant way is by using regular expressions.&#13;&#10;&#13;&#10;I must note, that for better performance, I only use non capturing groupings: (?:... ) &#13;&#10;&#13;&#10;&#60;?php&#13;&#10;/**&#13;&#10; * Remove one or more keys from the URL&#13;&#10; * @author Mészáros Lajos György&#13;&#10; * @param $key : Key( string ) or keys( array of strings ) that you want to remove&#13;&#10; */&#13;&#10;function remove_key_from_url($key){&#13;&#10;&#9;if(is_array($key)) $key = \&#39;(?:\&#39;.implode(\&#39;|\&#39;, $key).\&#39;)\&#39;;&#13;&#10;&#9;$url = preg_replace(\&#39;/(?:&#38;|(?&#60;=\\?))\&#39;.$key.\&#39;(?:=[^&#38;]*?)?(?=(?:&#38;|$))/\&#39;, \&#39;\&#39;, $_SERVER[\&#39;REQUEST_URI\&#39;]);&#13;&#10;&#9;return preg_replace(\&#39;/(?&#60;=\\?)&#38;|\\?$/\&#39;, \&#39;\&#39;, $url);&#13;&#10;}&#13;&#10;?&#62;&#13;&#10;&#13;&#10;So let\&#39;s assume you have the following URL:&#13;&#10;something.php?a=12&#38;b=134&#38;c=foo&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;echo remove_key_from_url(\&#39;c\&#39;);&#13;&#10;// will become: something.php?a=12&#38;b=134&#13;&#10;&#13;&#10;echo remove_key_from_url(\&#39;a\&#39;);&#13;&#10;// will become: something.php?b=134&#38;c=foo&#13;&#10;// Note: if key is next to ?, then it will remove the following &#38; sign, so the URL will not become something.php?&#38;b=134&#38;c=foo - ( that is what the 3rd line is for )&#13;&#10;&#13;&#10;echo remove_key_from_url(Array(\&#39;a\&#39;, \&#39;b\&#39;, \&#39;c\&#39;));&#13;&#10;// will become: something.php&#13;&#10;// Note: the 3rd line will remove the single ? from the end&#13;&#10;&#13;&#10;// current url here is: /foo/bar/?something&#38;a=3&#13;&#10;echo remove_key_from_url(\&#39;something\&#39;);&#13;&#10;// will become: /foo/bar/?a=3&#13;&#10;// Note: the code can handle if no value is assigned to a key&#13;&#10;?&#62;&#13;&#10;&#13;&#10;After this, you only need to pass the returned string to a header and then kill the page:&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;header(&#39;Location:&#39;.remove_key_from_url(&#39;logout&#39;));&#13;&#10;die();&#13;&#10;?&#62;&#13;&#10;&#13;&#10;I need to add, that I didn\&#39;t do any benchmarks upon testing whether is this the faster or doing a bunch of explodes and working with arrays.&#13;&#10;Also, I don\&#39;t think it is possible to combine the two regexes into a single one, because the regex in the 3rd line needs to be executed after all key-value removals are done, but if possible, please give me an e-mail, I\&#39;m really interested.&#13;&#10;&#13;&#10;Louis from Hungary --was-- A way of removing keys from $_GET - in my opinion - in a more elegant way is by using regular expressions. I must note, that for better performance, I only use non capturing groupings: (?:... ) <?php /** * Remove one or more keys from the URL * @author Mészáros Lajos György * @param $key : Key( string ) or keys( array of strings ) that you want to remove */ function remove_from_url($key){ if(is_array($key)) $key = '(?:'.implode('|', $key).')'; $url = preg_replace('/(?:&|(?<=\?))'.$key.'(?:=[^&]*?)?(?=(?:&|$))/', '', $_SERVER['REQUEST_URI']); return preg_replace('/(?<=\?)&|\?$/', '', $url); } ?> So let's assume you have the following URL: something.php?a=12&b=134&c=foo <?php echo remove_key_from_url('c'); // will become: something.php?a=12&b=134 echo remove_key_from_url('a'); // will become: something.php?b=134&c=foo // Note: if key is next to ?, then it will remove the following & sign, so the URL will not become something.php?&b=134&c=foo - ( that is what the 3rd line is for ) echo remove_key_from_url(Array('a', 'b', 'c')); // will become: something.php // Note: the 3rd line will remove the single ? from the end // current url here is: /foo/bar/?something&a=3 echo remove_key_from_url('something'); // will become: /foo/bar/?a=3 // Note: the code can handle if no value is assigned to a key ?> After this, you only need to pass the returned string to a header and then kill the page: <?php // current URL is admin.php?logout header('Location'.remove_key_from_url('logout')); ?> I need to add, that I didn't do any benchmarks upon testing whether is this the faster or doing a bunch of explodes and working with arrays. Also, I don't think it is possible to combine the two regexes into a single one, because the regex in the 3rd line needs to be executed after all key-value removals are done, but if possible, please give me an e-mail, I'm really interested. Louis from Hungary http://php.net/manual/en/reserved.variables.get.php

« previous php.notes (#187272) next »