tags:

views:

74

answers:

4
+3  Q: 

urlencode except /

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

like

urlencode('/this/is/my/file right here.jpg');
+7  A: 

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.

Artefacto
+2  A: 

This should solve your problem.

str_replace("%2F","/",urlencode('/this/is/my/file right here.jpg'));
Sam152
+2  A: 
$array = explode('/', '/this/is/my/file right here.jpg');
foreach ($array as &$value) {
        $value = urlencode($value);
}
print implode('/', $array);
David Dorward
+5  A: 

You can use

So all together in one line:

$path = implode('/', array_map('rawurlencode', explode('/', $path)));
Gumbo
This would be the most conceptually correct way of doing it, though in practice the `str_replace` would be simpler. Either way +1 for `rawurlencode`; it is always the right thing, whereas `urlencode` is sometimes the wrong thing.
bobince