Last active
December 21, 2018 20:56
-
-
Save ktamas77/49a1bc7af7add74863c4ace9d15e28bf to your computer and use it in GitHub Desktop.
Update Query Parameter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function update_query_param($url, $name, $value) { | |
$parsed_url = parse_url($url); | |
list($scheme, $host) = array_values($parsed_url); | |
$path = ''; | |
if (isset($parsed_url['path'])) { | |
$path = $parsed_url['path']; | |
} | |
if (!isset($parsed_url['query'])) { | |
return $url; | |
} | |
$query = $parsed_url['query']; | |
parse_str($query, $params); | |
$params[$name] = $value; | |
$updated_params = http_build_query($params); | |
$fragment = ''; | |
if (isset($parsed_url['fragment'])) { | |
$parsed_fragment = $parsed_url['fragment']; | |
$fragment = "#parsed_fragment"; | |
} | |
$updated_url = "$scheme://$host/$path?$updated_params$parsed_fragment"; | |
return $updated_url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment