Jump to content

Recommended Posts

I have a method that I can't seem to get to work.

public function query_remove($key){
$str = "";
$key_value = explode("&", $query_string);
foreach($key_value as $kv){
	list($k, $v) = explode("=", $kv);
	if($key != $k)
		$str .= "&$k=$v";
}
$this->url_query = ltrim($str, "&");
return $this;
}

 

Basically I want to take the current URL Query String (GET) and remove elements from it. I can get it to remove one element, but I can't remove 2+.

 

Query String:

elementA=valueA&elementB=valueB&elementC=valueC

 

Works:

$myclass->query_remove('elementA');

New String:

elementB=valueB&elementC=valueC

 

Doesn't Work:

$myclass->query_remove('elementA')->query_remove('elementB');

New String:

elementA=valueA&elementC=valueC

 

As you can see, elementA is still there, but should have been removed. Any suggestions how I could get this to work the way I want? I also would like it to work individually, so I could call it 10 different times if I wanted.

 

// Original String: elementA=valueA&elementB=valueB&elementC=valueC
echo $myclass->query_remove('elementA')->query_remove('elementB'); //now: elementC=valueC
echo $myclass->query_remove('elementA')->query_remove('elementC'); //now: elementB=valueB
echo $myclass->query_remove('elementB')->query_remove('elementC'); //now: elementA=valueA
echo $myclass->query_remove('elementB') //now: elementA=valueA&elementC=valueC

Link to comment
https://forums.phpfreaks.com/topic/238672-remove-values-from-get/
Share on other sites

That's an awful method you are using.

 

You can add this to your class. The function param is an array of the elements that you wish to remove:

 

<?php	
function query_remove($string, array $elements) {
$parts = explode('&', $string);
$new_query = array();
foreach($parts as $param_val) {
	$param_val_parts = explode('=', $param_val);
	if(!in_array($param_val_parts[0], $elements)) {
		$new_query[] = $param_val;
	}
}
return implode('&', $new_query);
}



$string = 'elementA=valueA&elementB=valueB&elementC=valueC';

echo "<p>".query_remove($string, array('elementA','elementC'))."</p>";
echo "<p>".query_remove($string, array('elementB','elementC'))."</p>";
echo "<p>".query_remove($string, array('elementA'))."</p>";
?>

public function query_remove($key) {
   parse_str($this->url_query, $get_array);
   unset($get_array[$key]);
   $this->url_query = http_build_query($get_array);
   return $this;
}

public function query_add($key, $val) {
   parse_str($this->url_query, $get_array);
   $get_array[$key] = $val;
   $this->url_query = http_build_query($get_array);
   return $this;
}

 

But personally I would keep it as an array in the object and convert it to a query string when needed:

 

public function __tostring() {
   return http_build_query($this->get_array);
}

public function query_remove($key) {
   unset($this->get_array[$key]);
   return $this;
}

public function query_add($key, $val) {
   $this->get_array[$key] = $val;
   return $this;
}

echo $myclass->query_remove('elementA')->query_remove('elementB')->query_add('elementD', 'valueD');

But personally I would keep it as an array in the object and convert it to a query string when needed:

 

public function __tostring() {
   return http_build_query($this->get_array);
}

public function query_remove($key) {
   unset($this->get_array[$key]);
   return $this;
}

public function query_add($key, $val) {
   $this->get_array[$key] = $val;
   return $this;
}

echo $myclass->query_remove('elementA')->query_remove('elementB')->query_add('elementD', 'valueD');

 

Where/what is get_array?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.