Rebuild Querystring


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

Credit: FinalWebsites.com
The only argument is a comma separated list of querystring vars that you don't want to carry over. At minimum you should pass the name of the var that you're appending the existing querystring(s) to so it's not included twice in the URL.


Copy this code and paste it in your HTML
  1. //
  2. // Example: echo '<a href="?page='. $previousPage . rebuildQueryString("page,submit") .'">Prev</a>';
  3. // This would carry over any querystrings from the current URL, minus the current
  4. // $_GET['page'] and $_GET['submit'] keys and values if they exist
  5. //
  6. function rebuildQueryString($curr_vars='')
  7. {
  8. if(!empty($_SERVER['QUERY_STRING']))
  9. {
  10. $parts = explode("&", $_SERVER['QUERY_STRING']);
  11. $curr_vars = str_replace(" ", "", $curr_vars);
  12. $c_vars = explode(",", $curr_vars);
  13. $newParts = array();
  14.  
  15. foreach ($parts as $val)
  16. {
  17. $val_parts = explode("=", $val);
  18. if(!in_array($val_parts[0], $c_vars))
  19. {
  20. array_push($newParts, $val);
  21. }
  22. }
  23.  
  24. if(count($newParts) != 0)
  25. {
  26. $qs = "&".implode("&", $newParts);
  27. }
  28. else
  29. {
  30. return false;
  31. }
  32. return $qs;
  33. }
  34. else
  35. {
  36. return false;
  37. }
  38. }

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.