note 97496 modified in function.parse-url by danbrown

From: Date: Thu, 22 Apr 2010 23:09:46 +0000
Subject: note 97496 modified in function.parse-url by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-168529@lists.php.net to get a copy of this message
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. <?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) { if($value && !preg_match('/'.$key.'=/', $url)) { $url .= '&'.$key.'='.$value; } } return $url; } // page url: "http://www.example.com/page.php?p=5&show=list&style=23" $url = modify_url(array('p' => 4, 'show' => 'column')); // $url = "http://www.example.com/page.php?p=4&show=column&style=23" ?> --was-- 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. <?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) { if($value && !preg_match('/'.$key.'=/', $url)) { $url .= '&'.$key.'='.$value; } } return $url; } // page url: "http://www.123.com/page.php?p=5&show=list&style=23" $url = modify_url(array('p' => 4, 'show' => 'column')); // $url = "http://www.123.com/page.php?p=4&show=column&style=23" ?> http://php.net/manual/en/function.parse-url.php

« previous php.notes (#168529) next »