Jump to content

This is weird. Url parameter and & symbol issue.


man5

Recommended Posts

I didn't notice this until know.

 

Say I have a url like this.

http://www.yoursite.com?category_id=3&category_name= Grocers & Shoppers

When I get the the category name using  $_GET, it'll only echo 'Grocers' and not the full word 'Grocers & Shoppers'. 

 

Anyone can tell me what's happening?

Yes it should be urlencoded

 

 

I actually found a tutorial that does this.  I copied his code. It works. The only thing it doesn't do is replace the & symbol. 

 

Function

       public function createSlug($slug) {
		// Remove anything but letters, numbers, spaces, hypens
		// Remove spaces and duplicate dypens
		// Trim the left and right, removing any left over hypens
		
		$lettersNumbersSpacesHypens = '/[^\-\s\pN\pL]+/u';
		$spacesDuplicateHypens = '/[\-\s]+/';

		$slug = preg_replace($lettersNumbersSpacesHypens, '', mb_strtolower($slug, 'UTF-8'));	
		$slug = preg_replace($spacesDuplicateHypens, '-', $slug);
		$slug = trim($slug, '-');
		
		return $slug;
	}

Using the above function, it'll return this

http://www.yourwebsite.com?category_id=1&category_name=hot-amp-dogs

from this

http://www.yourwebsite.com?category_id=1&category_name=hot & dogs

I am guessing 'amp' shouldn't be there.  Also since I am echoing these out on the page, they have to be decoded to it's original form.

I've solved the problem with the 'amp' removal.

 

Here is the updated function.

public function createSlug($slug) {
		// Remove anything but letters, numbers, spaces, hypens
		// Remove spaces and duplicate dypens
		// Trim the left and right, removing any left over hypens

		$slug   =   htmlspecialchars_decode($slug);
		
		$lettersNumbersSpacesHypens = '/[^\-\s\pN\pL]+/u';
		$spacesDuplicateHypens = '/[\-\s]+/';

		$slug = preg_replace($lettersNumbersSpacesHypens, '', mb_strtolower($slug, 'UTF-8'));	
		$slug = preg_replace($spacesDuplicateHypens, '-', $slug);
		$slug = trim($slug, '-');
		
		return $slug;
	}

Now for decoding. I have found out that I need to save two urls in db; original and slug. From there, I call on the original to echo the names.  So i'll look into how to do that.

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.