Hi guys,
I have a problem with sending spaces in a querystring being translated to underscores when reading with $GET.
If I use urlencode to encode the querystring I can see the spaces are replaced with a plus (+), so if my string is 'SOME STRING', it would be passed as 'SOME+STRING'.
If I then read the $
GET variables, I get 'SOME_STRING', not 'SOME+STRING'.

Is there a way around this?
I could translate underscores back to spaces myself, but what would happen if the string actually did contain an underscore?
For example, if I passed 'SOME_STRING', when I read the $_GET variables I would translate the underscore to a space which would be wrong.

Can anyone help me with the correct way of passing and reading querystring variables that contain spaces?
Thanks!

    The part of the querystring containing the space is actually the key rather than the value as follows:

    href="http://mysite.com/index.php?Item+Colour[]=24"

    Using this code to read the querystring:

      reset($_GET);
      while(list($key, $value) = each ($_GET)) {
      .
      .
      .
      }
    

    $key comes back as Item_Colour

      It's a utility I've written for an open source cart which allows users to define their own variable names....unfortunately some have defined them with spaces and are getting problems!
      I'm thinking maybe I should just remove all spaces rather than let php translate them to underscores!

        damiantaylor;11000925 wrote:

        ...which allows users to define their own variable names....

        My spider sense is tingling...

          Just remembered I need to leave the variable name as it is because I need it to look up on the database.....it looks like I'm going to have to say my utility doesn't support variable names with a space!

          The only other thing I can think to do is translate spaces to some random character like ~ then I can pass it and translate it back for database lookups.

          Anybody got a better idea?
          Stop writing software maybe? :evilgrin:

            Unfortunately I don't validate the names, it's the cart that does that and the cart allows you to put pretty much anything in there 🙁

            If I remove the spaces behind the scenes, I don't know where to re-add them to do the database lookup unless I use REPLACE() in the where clause to remove spaces as I read the database too.

            It's either that or replace spaces with random characters I think?

              Thanks bradgrafelman, looks like we were typing at the same time!
              I'll have a play around and use one of your suggestions.

              Thanks to everyone who had input!

                Write a Reply...