PHP Add and Remove Querystring Variables


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



Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.