1

I have been given some instructions to percent encode a URL twice. I know how to percent encode a URL once but how do you do it twice?

Surly when it is encoded once, it will be the same when encoded again.

Have I missed something?

Instructions or algorithm would be great!

John Wheal
  • 9,908
  • 6
  • 29
  • 39

2 Answers2

5

It won't be the same since you encode the % used for encoding.

$url = 'http://www.youtube.com/watch?v=35_0IN36rUI'
echo $url;
echo urlencode($url);
echo urlencode(urlencode($url));

will give:

http://www.youtube.com/watch?v=35_0IN36rUI
http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D35_0IN36rUI
http%253A%252F%252Fwww.youtube.com%252Fwatch%253Fv%253D35_0IN36rUI
Tchoupi
  • 14,560
  • 5
  • 37
  • 71
4

To doubly encode the Url in php do:

$encodedUrl = urlencode(urlencode($url));

Definitely not the same output when encoded twice. The first adds percent encodings and the second will actually encode those percent signs... For example:

urlencode('guts & glory'); // "guts+%26+glory"
urlencode(urlencode('guts & glory')); // "guts%2B%2526%2Bglory"
Prestaul
  • 83,552
  • 10
  • 84
  • 84