note 98468 deleted from function.parse-url by danbrown

From: Date: Thu, 17 Jun 2010 16:04:18 +0000
Subject: note 98468 deleted from function.parse-url by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170208@lists.php.net to get a copy of this message
Note Submitter: info at 26design dot com ---- Regarding the modify_url function above, I updated it slightly in order to include the ability to add a new key/value pair, in case nothing was there before. Original description from solenoid at example dot com (see http://www.php.net/manual/en/function.parse-url.php#97496): "Here's a piece of code that modifies, replaces or removes the url query. This can typically used in paging situations where there are more parameters than the page." Modified version: <?php function modify_url($mod) { $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; $query = explode("&", $_SERVER['QUERY_STRING']); // modify/delete data foreach($query as $q) { list($key, $value) = explode("=", $q); if(array_key_exists($key, $mod)) { if($mod[$key]) { $url = preg_replace('/'.$key.'='.$value.'/', $key.'='.$mod[$key], $url); } else { $url = preg_replace('/&?'.$key.'='.$value.'/', '', $url); } } } // add new data foreach($mod as $key => $value) { // updated 06/15/2010 if ( $value ) { if($query[0] && !preg_match('/'.$key.'=/', $url)) { $url .= '&'.$key.'='.$value; } else if (!$query[0]) { $url .= '?'.$key.'='.$value; } } } return $url; } // page url: ".../page.php?p=5&show=list&style=23" $url = modify_url(array('p' => 4, 'show' => 'column')); // $url = ".../page.php?p=4&show=column&style=23" ?>

« previous php.notes (#170208) next »