View Full Version : Passing a variable with an & through $_GET

09-05-2007, 10:01 PM
I'm having some trouble passing a variable through $_GET.

The variable is

$category = "Business & Entrepreneurial";

It needs to be passed from one page to another using $_GET.

However, because the value has an ampersand in it, only the portion of the value before the ampersand is transferred.

So, for "Business & Entrepreneurial", the value of $category is transferred simply as "Business ".

I have to pass along a few values with this sort of misleading punctuation, including ampersands forward slashes.

I know that & is the html code for and ampersand, but it won't help much to use that since it actually has an ampersand in it and creates the same problem.

Does anyone know of a fix or work around?


09-05-2007, 10:08 PM
Change the ampersand to "%26", or use urlencode() (http://www.php.net/manual/en/function.urlencode.php):

$category = urlencode("Business & Entrepreneurial");

09-05-2007, 10:08 PM

09-05-2007, 10:39 PM
Thanks so much for the quick responses.

I love finding out that the makers of php have created a function for exactly what I need. However, the problem is actually a bit more complicated than I let on.

I'm actually using mod_rewrite to pass $_GET variables. (just when I thought I was done with all of those annoying .htaccess problems).

The rewrite rule is this:

RewriteRule browse/(.+)/?$ /browsecategory.php?category=$1 [L,NC,NE]

Entering the real URL (as encoded by urlencode) into the browser works fine, so:
is all good.

However, typing
causes the value of $_GET["category"] to set only as "Business"

The rewrite rule does work for categories with no punctuation.

Apparently the rule is being thrown off by the urlencoding, much like the $_GET array was thrown off by an ampersand.

Ugh. Anyone have any ideas?

09-06-2007, 01:01 AM
Got it.

Found a tip online that said to use urlencode twice. For some reason this works.