Rebuild query string
We can use the following function to rebuild the query string. The function is very useful for pagination or passing variables from one page to another.In this function it is possible to add more than one variable names as functions arguments. This names will be filtered from the new generated query string. We can just add the variable name no need a comma seperated string.
<?php
function rebuild_qs($curr_vars) {
if (!empty($_SERVER[’QUERY_STRING’])) {
$parts = explode(”&”, $_SERVER[’QUERY_STRING’]);
$curr_vars = str_replace(” “, “”, $curr_vars); // remove whitespace
$c_vars = explode(”,”, $curr_vars);
$newParts = array();
foreach ($parts as $val) {
$val_parts = explode(”=”, $val);
if (!in_array($val_parts[0], $c_vars)) {
array_push($newParts, $val);
}
}
if (count($newParts) != 0) {
$qs = “&”.implode(”&”, $newParts);
} else {
return false;
}
return $qs; // this is your new created query string
} else {
return false;
}
}
/* Example:
script.php?ident=1<?php echo rebuild_qs(”ident, submit, var_one”); ?> */
?>
