How to encode URL using php like browsers do

我们两清 提交于 2019-11-27 02:49:06

问题


I have an URL like this

http://www.example.com/Data/image/office-dôn-sì-à.jpg

I want to copy that file to my server using copy function in php. So the first thing is to encode it to this (I think browsers do the same thing)

http://www.example.com/Data/image/office-d%C3%B4n-s%C3%AC-%C3%A0.jpg

But if I use function urlencode, full url will be encoded to

http%3A%2F%2Fwww.example.com%2FData%2Fimage%2Foffice-d%C3%B4n-s%C3%AC-%C3%A0.jpg

which is not an URL anymore and not what I want.

Any help?


回答1:


So, the other answers here have largely ignored your post, it seems. Let's hope I have not done the same.

It seems to me that you only want to encode the basename? If that is true, this ad-hoc function should do the trick:

function encode_basename($url) {
    $url = explode('/', $url);
    $base = array_pop($url);

    return implode('/', $url) . '/' . urlencode($base);
}
//returns: http://www.example.com/Data/image/office-d%25C3%25B4n-s%25C3%25AC-%25C3%25A0.jpg


来源:https://stackoverflow.com/questions/5441667/how-to-encode-url-using-php-like-browsers-do

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