Posted By

DaveChild on 09/11/08


Tagged

querystring


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

DaveChild
xmartyxcorex
silentbob84
crunch
piepkrak


PHP Add and Remove Querystring Variables


 / Published in: PHP
 

URL: http://www.addedbytes.com/php/querystring-functions/

  1. function add_querystring_var($url, $key, $value) {
  2. $url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
  3. $url = substr($url, 0, -1);
  4. if (strpos($url, '?') === false) {
  5. return ($url . '?' . $key . '=' . $value);
  6. } else {
  7. return ($url . '&' . $key . '=' . $value);
  8. }
  9. }
  10.  
  11. function remove_querystring_var($url, $key) {
  12. $url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
  13. $url = substr($url, 0, -1);
  14. return ($url);
  15. }

Report this snippet  

You need to login to post a comment.