note 103686 added to function.parse-url

From: Date: Thu, 28 Apr 2011 06:04:39 +0000
Subject: note 103686 added to function.parse-url
Groups: php.notes 
Request: Send a blank email to php-notes+get-179219@lists.php.net to get a copy of this message
@ 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; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 75.81.20.78 ---- Manual Page -- http://www.php.net/manual/en/function.parse-url.php Edit -- https://master.php.net/note/edit/103686 Del: integrated -- https://master.php.net/note/delete/103686/integrated Del: useless -- https://master.php.net/note/delete/103686/useless Del: bad code -- https://master.php.net/note/delete/103686/bad+code Del: spam -- https://master.php.net/note/delete/103686/spam Del: non-english -- https://master.php.net/note/delete/103686/non-english Del: in docs -- https://master.php.net/note/delete/103686/in+docs Del: other reasons-- https://master.php.net/note/delete/103686 Reject -- https://master.php.net/note/reject/103686 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#179219) next »