Rebuild query string


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. <?php
  2. function rebuild_qs($curr_vars) {
  3. if (!empty($_SERVER['QUERY_STRING'])) {
  4. $parts = explode("&", $_SERVER['QUERY_STRING']);
  5. $curr_vars = str_replace(" ", "", $curr_vars); // remove whitespace
  6. $c_vars = explode(",", $curr_vars);
  7. $newParts = array();
  8. foreach ($parts as $val) {
  9. $val_parts = explode("=", $val);
  10. if (!in_array($val_parts[0], $c_vars)) {
  11. array_push($newParts, $val);
  12. }
  13. }
  14. if (count($newParts) != 0) {
  15. $qs = "&".implode("&", $newParts);
  16. } else {
  17. return false;
  18. }
  19. return $qs; // this is your new created query string
  20. } else {
  21. return false;
  22. }
  23. }
  24. /* Example:
  25. script.php?ident=1<?php echo rebuild_qs("ident, submit, var_one"); ?> */
  26. ?>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.