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 applyrawurlencode
on each item (don’t useurlencode
, that’s only for application/x-www-form-urlencoded encoded query arguments!), andimplode
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);
精彩评论