开发者

urlencode except /

Is there a way to urlencode except directory seperators / in the path ?

l开发者_运维百科ike

urlencode('/this/is/my/file right here.jpg');


You can use

  • explode to split your path into the path segments,
  • array_map to apply rawurlencode on each item (don’t use urlencode, that’s only for application/x-www-form-urlencoded encoded query arguments!), and
  • implode to put the segments back together.

So all together in one line:

$path = implode('/', array_map('rawurlencode', explode('/', $path)));


Replace them again:

str_replace('%2F', '/', urlencode('/this/is/my/file right here.jpg'));

Note that if you are going to pass the result in a query string, you should not do the replacement above -- use only urlencode. If you are using it in the path portion, you ought to use rawurlencode instead.


This should solve your problem.

str_replace("%2F","/",urlencode('/this/is/my/file right here.jpg'));


$array = explode('/', '/this/is/my/file right here.jpg');
foreach ($array as &$value) {
        $value = urlencode($value);
}
print implode('/', $array);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜