1

Among other differences, urlencode and rawurlencode encode spaces differently. + and %20 resepctively. According to the PHP docs, variables are decoded automatically. How does PHP “know” which way to decode it?

I used to use urlencode until I read here on SO that the raw counterpart is generally better. I must say, I've never run into any decoding problems but am curious as to how it works and if you think there are any scenarios when rawurlencode would cause issues due to the automatic decoding.

Thanks.

texelate
  • 2,460
  • 3
  • 24
  • 32
  • http://stackoverflow.com/a/996161/1207346 – Dale Jul 08 '13 at 10:26
  • Personally urlencode and urldecode never gave me any problems, I've never had to use rawurlencode. – casraf Jul 08 '13 at 10:26
  • Values in `$_GET` seem to be automatically decoded according to *both* standards, i.e. both `%20` and `+` are spaces. Which is pretty weird, actually. – deceze Jul 08 '13 at 10:29

1 Answers1

2

The + character is encoded by both function as %2B, so no confusion is possible.

To safely decode any version, PHP only has to transform each %XX into its corresponding character and transform each + to a space. This is what urldecode does.

rawurlencode shouldn't cause issues as all it does is encode a wider range of chars into their %XX counterparts. Those will be decoded safely by any version of the function.

thibauts
  • 1,638
  • 9
  • 8