Is there a way to urlencode except directory seperators / in the path ?
like
urlencode('/this/is/my/file right here.jpg');
Is there a way to urlencode except directory seperators / in the path ?
like
urlencode('/this/is/my/file right here.jpg');
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);
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!), andimplode
to put the segments back together.So all together in one line:
$path = implode('/', array_map('rawurlencode', explode('/', $path)));