如何编码包含正斜杠的查询字符串


How do I encode a query string containing forward slashes?

我需要将路径作为参数传递给url,例如:/Controller/Action/My-Path,但My-Path包含正斜杠,例如/dir1/dir2/file.php是否有任何方法将此路径作为单个参数传递,或者我需要将路径分解为/Controller/Action/Param1/dir1/Param2/dir2/Param3/file.php。如能提供一些示例代码将不胜感激

TIA以法莲

你可以使用url视图助手,例如:

    echo $this->view->url(
            array(
            'controller' => 'somecontroller',   
            'action' => 'someaction',    
            'path'=>'/dir1/dir2/file.php')
            );

这将导致:

public/somecontroller/someaction/path/%2Fdir1%2Fdir2%2Ffile.php

url视图助手自动使用url_encode对参数进行编码(参见其他答案)。

见:http://ar2.php.net/url_encode

所以你会这样做:

$param = url_encode("/this/parameter/file");