bubblesnout Posted September 10, 2009 Share Posted September 10, 2009 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 More sharing options...
gr1zzly Posted September 11, 2009 Share Posted September 11, 2009 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. Link to comment https://forums.phpfreaks.com/topic/173810-_get-array-and-urlencoded-ampersands/#findComment-916638 Share on other sites More sharing options...
kratsg Posted September 11, 2009 Share Posted September 11, 2009 Wouldn't it be easier to replace the '&' with "and" and then replace it back afterwards? Link to comment https://forums.phpfreaks.com/topic/173810-_get-array-and-urlencoded-ampersands/#findComment-916698 Share on other sites More sharing options...
p2grace Posted September 11, 2009 Share Posted September 11, 2009 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. Link to comment https://forums.phpfreaks.com/topic/173810-_get-array-and-urlencoded-ampersands/#findComment-916701 Share on other sites More sharing options...
thebadbad Posted September 11, 2009 Share Posted September 11, 2009 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? Link to comment https://forums.phpfreaks.com/topic/173810-_get-array-and-urlencoded-ampersands/#findComment-916747 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.