问题
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!
回答1:
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
回答2:
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"
来源:https://stackoverflow.com/questions/10112447/percent-encode-url-twice