Percent-encode URL Twice

大城市里の小女人 提交于 2019-12-24 15:36:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!