man5 Posted April 8, 2014 Share Posted April 8, 2014 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? Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted April 8, 2014 Share Posted April 8, 2014 (edited) Yes it should be urlencoded Edited April 8, 2014 by QuickOldCar Quote Link to comment Share on other sites More sharing options...
man5 Posted April 8, 2014 Author Share Posted April 8, 2014 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. Quote Link to comment Share on other sites More sharing options...
man5 Posted April 8, 2014 Author Share Posted April 8, 2014 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.