除了目录分隔符/路径之外,有没有办法到urlencode?
喜欢
urlencode('/this/is/my/file right here.jpg');
Run Code Online (Sandbox Code Playgroud)
Gum*_*mbo 34
您可以使用
explode 将路径分割为路径段,array_map适用rawurlencode于每个项目(不使用urlencode,这只是为应用程序/ x-WWW的形式了urlencoded编码的查询参数!),和implode 把段重新组合在一起.所以一起排在一行:
$path = implode('/', array_map('rawurlencode', explode('/', $path)));
Run Code Online (Sandbox Code Playgroud)
Art*_*cto 15
再次替换它们:
str_replace('%2F', '/', urlencode('/this/is/my/file right here.jpg'));
Run Code Online (Sandbox Code Playgroud)
请注意,如果要将结果传递给查询字符串,则不应执行上面的替换 - 仅使用urlencode.如果您在路径部分中使用它,则应该使用rawurlencode.