Voting

: one minus one?
(Example: nine)

The Note You're Voting On

jesse at example dot com
14 years ago
@ solenoid: Your code was very helpful, but it fails when the current URL has no query string (it appends '&' instead of '?' before the query). Below is a fixed version that catches this edge case and corrects it.

<?php
function modify_url($mod)
{
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$query = explode("&", $_SERVER['QUERY_STRING']);
if (!
$_SERVER['QUERY_STRING']) {$queryStart = "?";} else {$queryStart = "&";}
// 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 .= $queryStart.$key.'='.$value;
}
}
return
$url;
}
?>

<< Back to user notes page

To Top