HelpNeeder
HelpNeeder

Reputation: 6490

Reading & character that is being passed through GET

I am reading content of GET query string, and every time I encounter & for ecample Blackstone Woodfire & Grill, GET is reading Blackstone Woodfire.

How can I avoid this, if possible?

I know I could encode the special characters from the reference page, then decode them when are directed to this page.

I'm just curious.

Upvotes: 0

Views: 40

Answers (2)

Arron
Arron

Reputation: 916

The problem is that the parameters you send using get, are separated using a &. So if you have an url like

 http:/example.com?param_1=value_1&param_2=value_2

You will have an $_GET array like

array(
    param_1 => 'value_1',
    param_2 => 'value_2'
);

Now if you send and url like:

 http://example.com?param_1=value_1 & value_2

You will have an $_GET array like

array(
    param_1 => 'value_1 ',
    ' value_2' => ''
);

Simply becuase that is the way sending GET params works. On the recieving side, there is not much you can do, the problem lies at the other end. The GET parameters that are beeing send must indeed be encoded, within PHP that is done using

 echo 'http://example.com?param_1=' . urlencode('value_1 & value_2');

Javascript uses encodeURIComponent() to solve this issue. PHP calles urldecode() automaticly on every get parameter when it is creating your $_GET global.

Upvotes: 1

Lauri Orgla
Lauri Orgla

Reputation: 561

You could use urlencode to encode the get string. And later if u want to fetch it from $_GET u urldecode.

You could replace all ampersands to %26

Upvotes: 0

Related Questions