php - Reading & character that is being passed through GET

522

I am reading content ofGET query string, and every time I encounter& for ecampleBlackstone Woodfire & Grill,GET is readingBlackstone 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.

482

Answer

Solution:

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.

425

Answer

Solution:

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

People are also looking for solutions to the problem: php - Filter an array of objects by a flat array

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.