note 98469 added to function.parse-url
| From: | info at 26design dot com | Date: | Thu, 17 Jun 2010 16:03:45 +0000 |
| Subject: | note 98469 added to function.parse-url | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170207@lists.php.net to get a copy of this message | ||
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 #97496 above): "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: "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"
?>
----
Server IP: 64.71.164.2
Probable Submitter: 209.163.156.82
----
Manual Page -- http://www.php.net/manual/en/function.parse-url.php
Edit -- https://master.php.net/note/edit/98469
Del: integrated -- https://master.php.net/note/delete/98469/integrated
Del: useless -- https://master.php.net/note/delete/98469/useless
Del: bad code -- https://master.php.net/note/delete/98469/bad+code
Del: spam -- https://master.php.net/note/delete/98469/spam
Del: non-english -- https://master.php.net/note/delete/98469/non-english
Del: in docs -- https://master.php.net/note/delete/98469/in+docs
Del: other reasons-- https://master.php.net/note/delete/98469
Reject -- https://master.php.net/note/reject/98469
Search -- https://master.php.net/manage/user-notes.php