Skip to content

Instantly share code, notes, and snippets.

@bradym
Last active August 29, 2015 14:27
Show Gist options
  • Save bradym/52946884896990b4a820 to your computer and use it in GitHub Desktop.
Save bradym/52946884896990b4a820 to your computer and use it in GitHub Desktop.
<?php
function modify_url($mod, $url = FALSE){
// If $url wasn't passed in, use the current url
if($url == FALSE){
$scheme = $_SERVER['SERVER_PORT'] == 80 ? 'http' : 'https';
$url = $scheme.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
// Parse the url into pieces
$url_array = parse_url($url);
// The original URL had a query string, modify it.
if(!empty($url_array['query'])){
parse_str($url_array['query'], $query_array);
foreach ($mod as $key => $value) {
if(!empty($query_array[$key])){
$query_array[$key] = $value;
}
}
}
// The original URL didn't have a query string, add it.
else{
$query_array = $mod;
}
return $url_array['scheme'].'://'.$url_array['host'].'/'.$url_array['path'].'?'.http_build_query($query_array);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment