Jump to content

$_GET array and urlencoded ampersands


bubblesnout

Recommended Posts


Firstly, Hi everybody here! I hope to hang around here, because I'm starting to develop in PHP more and more these days. Anyway, here's my query.

 

I'm using values from a database to construct a url with a querystring, being used as a link on a page. Because this value may contain things such as an ampersand (&), I figured I would be right in using urlencode(), or even better, rawurlencode(). Here's an example of the outputted HTML:

 

<a href="?page=music&group=band&value=Conor%20Oberst%20%26%20The%20Mystic%20Valley%20Band">Conor Oberst & The Mystic Valley Band</a>

 

It looks as though the 'value' has been encoded perfectly, with the ampersand being replaced with %26. However my problem is this. Even with this encoded url, PHP is splitting the string into the $_GET array at the %26. From that url, here's an output of print_r

 

Array

(

   


=> music

    [group] => band

    [value] => Conor Oberst

    [The_Mystic_Valley_Band] =>

)

 

As far as I've read, this is NOT how php is supposed to handle it. Although it is doing this both on my local WAMP installation, and also on my Unix server running apache.

 

Any thoughts?

Link to comment
https://forums.phpfreaks.com/topic/173810-_get-array-and-urlencoded-ampersands/
Share on other sites

Since you want the ampersand in this case to be a part of the string "Conor Oberst & The Mystic Valley Band" you need to escape this character in some way so that it is not treated as part of the query and simply as part of the string to be returned.

 

I'm not an expert so I don't know if the standard escape character ' \ ' (before url encoding) would work or if there is some other way to prevent this character from being encoded into it's url form.

If you didn't want to replace it with "and" since that could cause actual instances of the word "and" to be converted when they shouldn't have... you could replace it with a some other unique string that wouldn't cause any potential erroneous conversions.

As far as I've read, this is NOT how php is supposed to handle it. Although it is doing this both on my local WAMP installation, and also on my Unix server running apache.

 

Any thoughts?

 

You're right, that's not how it's supposed to work, and it's not how it works on my servers (PHP 5.2.3 @ Apache; PHP 5.2.6 @ Apache). Either it's a bug, or you're doing it wrong somehow. Have you tried to copy and paste the URL to the address bar, and then see what print_r() says?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.