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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.