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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.