Jump to content

Trying to unset values but having problems with spaces


ukweb

Recommended Posts

Hi all

 

I have constructed for a client a vehicle search engine. If a make is defined it obviously searches by make, and works as expected. However, I have a form which unsets the make variable so that the results can be returned without the make, and also works... providing there is no spaces in the makes name!

 

it works by passing a variable in the url called 'rem' and if 'rem=make' it unsets the make variable, but when there are spaces it does unset it, but it doesn't remove the 'make=blah blah' from the $new_return variable...

 

Here's my code:

 

// If Make is to be unset...
if ($_GET['rem'] == 'make') {

	$this_make = str_replace(' ', '+', $_GET['make']);
	$this_make = str_replace('%20', '+', $_GET['make']);

	list($new_return1,$new_return2) = explode('&make='.$this_make,$new_return); 
	$new_return = $new_return1.$new_return2;

	list($new_return1,$new_return2) = explode('&rem='.$_GET['rem'],$new_return); 
	$new_return = $new_return1.$new_return2;

	unset($_GET['make']);	
}

 

I hope this makes sense! ANY help would be received with gratitude.

Spaces are not valid within a url. Use url_encode and url_decode.

 

Ok, using urldecode has helped, and I have rewritten the code so it reads:

 

		$this_model = urldecode($_GET['model']);
	$this_model = str_replace(' ', '+', $this_model);
	$new_return = str_replace('model='.$this_model, '', $new_return);
	$new_return = str_replace('rem=model', '', $new_return);

	// URL cleaning
	$new_return = str_replace('&&', '&', $new_return);
	$new_return = str_replace('=&', '=', $new_return);

	print $new_return; // debugging
	unset($_GET['model']);	

 

This works ok, but doesn't if an '&' is left at the end of $new_return.

 

Does anyone know how to do this:

 

if (last character of string $new_return == '&') { remove '&' from end of $new_return }

 

thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.