Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kennard McGill
Full Stack JavaScript Techdegree Graduate 45,227 PointsDecode special characters in URL
I'm currently developing a site but need help changing the encoded characters in the url. I've tried a RewriteRule but can't seem to get it to work. Perhaps there is even a better way of handling this.
My urls started out as store/gifts/shop.php?id=Harry+%26+David
After my RewriteRule in my .htaccess file the url is now
stores/gifts/Harry+%26+David
But how do I clean up the URl so it doesn't display encoded characters. Its Harry & David in my database field
4 Answers
Kennard McGill
Full Stack JavaScript Techdegree Graduate 45,227 PointsI've tried that but but php just returns:
'Harry' instead of 'Harry & David'
Since this is my id value I need it entack to pull the shops detail page.
I'm thinking it might just be better to change the character in my database from ampersand '&' to a dash '-'. At least it will still be search engine friendly if its Harry-David, which is my ultimate goal.
I'm not sure what to do with it really. Is leaving it there with the encoded character considered bad coding or is it ok to have characters like that in the url.
Thanks Randy!
Randy Hoyt
Treehouse Guest TeacherHey Kennard McGill ,
You should be able to use the following function in PHP:
$id = urldecode($id);
Here's a link to the documentation for it: http://php.net/manual/en/function.urldecode.php.
Does that help?
Kennard McGill
Full Stack JavaScript Techdegree Graduate 45,227 PointsI'm sorry if i wasn't clear on my first question. I can display the character just fine on the page, it was just the url i was concerned about and wanted to make sure i was following best pratices. Adding a second field like you stated is a great way to handle this.
Thanks Randy!
Faizal Heesyam
8,843 Pointsor maybe you want to slugify that string..
function slugify($string) {
return strtolower(trim(preg_replace(array('~[^0-9a-z]~i', '~-+~'), '-', $string), '-'));
}
:D
Kennard McGill
Full Stack JavaScript Techdegree Graduate 45,227 PointsSorry it took so long to respond Faizal, but thanks for the function!
Randy Hoyt
Treehouse Guest TeacherRandy Hoyt
Treehouse Guest TeacherFirst of all, you can certainly have an escaped ampersand in the URL. I'm surprised it's not working. Ampersands are tricky because they are used in HTML to mark an entity, but I wouldn't think that would be a problem.
But second of all, I would shy away from using them for web address.
But third of all, I wouldn't change the name of the character in the database. You should be able to display it on the page as Harry & David if that's what you want it to say. The trick is to have two fields, one for display name and one for the web address. (This is what software like WordPress does.) The shirt could have two columns:
Does that help?