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
- explodeto split your path into the path segments,
- array_mapto apply- rawurlencodeon each item (don’t use- urlencode, that’s only for application/x-www-form-urlencoded encoded query arguments!), and
- implodeto 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);
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论