Welcome to WebmasterWorld Guest from 54.211.136.250

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Exploding URL that has plus sign - quick question

   
3:34 am on Dec 5, 2011 (gmt 0)



Just had a question out of curiosity...

I am trying to explode my URL into an array like:

array_pop(explode("+", $_GET['id']))

but wasn't working and my MySQL query was failing because no results were picking up.

I changed the code to:

array_pop(explode(" ", $_GET['id']))

and seemed to work fine...does PHP treat "+" as a blank space when using GET? And if so, why? Does it just recognize the plus sign as a normal URL parameter "joiner" and flush it out when you use GET?

Thanks :)
7:21 am on Dec 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



print/echo and see what values you are getting. For example:

$url_example = explode("+", $_SERVER['REQUEST_URI']);
print_r($url_example);

This works fine for a URL with + sign in it, and the print_r prints the array.
10:49 am on Dec 5, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Regardless of PHP... the "+" (plus) is a reserved character in the query part of the URL. And is used to encode a space in the querystring. PHP passes all GET values through urldecode() so the "+" gets converted (back) to a space.

In order to use a literal "+" in the querystring it needs to be encoded as "%2B". And PHP will decode this for you when read from the $_GET array.

urlencode('Hello+World') = 'Hello%2BWorld'
8:36 pm on Dec 5, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



It's very important to read the HTTP specs. They define what characters are valid in domain names, paths and query strings. All three things have different rules.
10:51 pm on Dec 5, 2011 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



does PHP treat "+" as a blank space when using GET

It may be more useful to look at it the other way around. A blank space in a query string is shown as + (alias %2B), which your php reads as the intended blank space. Conversely, a blank space in the body of the URL (bad! bad! bad! ;)) will change in transit into %20.
8:12 am on Dec 13, 2011 (gmt 0)



lol, ya I figured it wasn't a great way to go about it....and was getting confused when it worked with exploding an actual blank space (" ")...

Was able to modify my id in my database to explode a hyphen instead. Works like a charm and probably a better way of going about things.

I really appreciate everyone's input and explanations!

Also, started reading up on the HTTP specs. Thanks for pointing me towards the topic, g1smd :)