Posting a variable using $_GET and the $ character breaks it

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Posting a variable using $_GET and the $ character breaks it

Post by robster »

Hi all,

I am posting variable using similar to this:
<a href = \"index.php?category=Dogs & Cats\">

When the url comes thru, it looks similar to this:
index.php?category=Dogs%20&%20Cats and my code of $cat = $_GET['category'] returns $cat as 'Dogs' and misses out on the rest!

What normally happens, is my site then goes thru the database, and retrieves any entries with Dogs & Cats in it, except, the & is breaking the search.

If I were to do a search on a category without an &, IE:
<a href = \"index.php?category=Dog Biscuits\">
then it comes out index.php?category=Dog%20Biscuits and it works a treat, but I HAVE to have the & in the url.

How can I overcome this? All my database entries (nearly 4500 of them) have an & in them!

Thanks so much, I really appreciate your time and help :)
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

Was just thinking about this, the & character is used by PHP to define more variables...

So a URL like my one above would be sticking data after & into a variable (or at least trying, except it's a %20).

Is there any way to get around this? Seems a toughy, I could parse my database, string search for any & characters and replace them with a 'and' string?

Anyone think this is the best bet, or is there a way to use the data I am still using....

WHAT about (thinking out loud here).... keep the data in the database that I have (with the &'s in it) and when I retrieve it initially to render my menu, that is the time I change the '&' to an 'and', then, when it comes back as my $GET call, change it back to an '&' for use in the database.

I may try that, I'd appreciate any advice here though, maybe something more elegant, or is that perhaps as good as it gets? ;)

Thanks for reading, and I really love any advice given,
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

rawurlencode()
Post Reply