Return to Snippet

Revision: 5461
at March 10, 2008 10:37 by gbot


Initial Code
<?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"); ?> */
?>

Initial URL
http://www.finalwebsites.com/snippets.php?id=6

Initial Description
I modified some code created by Macromedia Dreamweaver into a flexible function to rebuild the query string. The function is very useful for pagination or passing variables from one page to another.In this version it's possible to add more than one variable names into the functions arguments. This names will be filtered from the new generated query string. Just add the variable names you don't need into a comma seperated string.

Initial Title
Rebuild query string

Initial Tags
query

Initial Language
PHP