We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

DaveChild on 09/11/08


Tagged

querystring


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

DaveChild


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.