note 103686 deleted from function.parse-url by danbrown

From: Date: Thu, 28 Apr 2011 15:58:39 +0000
Subject: note 103686 deleted from function.parse-url by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-179237@lists.php.net to get a copy of this message
Note Submitter: quixotiCfluX ---- @ solenoid @ jesse Here is also something to consider when modifying the query string. Instead of the heavy lifting, using parse_str and http_build_query. The result is much shorter and easier to understand what is going on in the code. parse_str turns ampersand separated key=value pairs into a named key array, and http_build_query turns an array back into ampersand key=value pairs. And the test isn't really needed, if a key in the source array matches one in the current query string it will get updated with the new value, and any unique pairs in the input array will get added. <?php function tweak_url($arr) { parse_str($_SERVER['QUERY_STRING'],$query); foreach($arr as $key => $value) { $query[$key] = $value; } $url = "http://" . $_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'] . "?" . http_build_query($query); return $url; } ?>

« previous php.notes (#179237) next »