Posted By

anagaiyahoocom on 06/11/11


Tagged

php


Versions (?)

Delete key=value from url query string


 / Published in: PHP
 

  1. /**
  2. * Remove key/value pair from url query string
  3. *
  4. * @param $qry string Query String
  5. * @param $key string Query key to remove
  6. * @return string Modified query string
  7. * @static
  8. */
  9.  
  10. function removeQueryVal($qry,$qryKey)
  11. {
  12. $qry_new = '';
  13.  
  14. if(strlen($qry)>1)
  15. {
  16. $qry = (strpos($qry,"?")!==false) ? substr($qry,1) : $qry;
  17.  
  18. $qryArr = (strpos($qry,'&')!==false) ? explode("&",$qry) : $qry;
  19.  
  20. //build query array with removed $key value
  21. foreach($qryArr as $val)
  22. {
  23. //ignore $key query value
  24. if(strpos($val,$qryKey.'=')===false)
  25. {
  26. $qryPair = explode('=',$val);
  27. $qryval[$qryPair[0]] = $qryPair[1];
  28. }
  29. }
  30.  
  31. //rebuild new query string
  32. foreach($qryval as $key=>$val)
  33. {
  34. $qry_new .= '&'.$key.'='.$val;
  35. }
  36. $qry_new = substr($qry_new,1);
  37.  
  38. $qry_new = ($qry_new) ? '?'.$qry_new : '';
  39. }
  40.  
  41. return $qry_new;
  42. }

Report this snippet  

You need to login to post a comment.