Processing URL parameters - name value pairs separated by slashes

丶灬走出姿态 提交于 2019-12-07 12:10:21

问题


I want to have URLs like :-

URL 1 -    www.projectname/module/search/param/1
URL 2 -    www.projectname/param/1/module/search

I want a PHP code which takes the above URLs as parameter and returns an array like

Array("module" => "search", "param" => 1) (for URL 1)
Array("param" => 1, "module" => "search") (for URL 2)

So that I can use the result as $_GET in my project. I came to know that it would be better and easier to do this with PHP than with htaccess rewrite rules. If you can help with rewrite rules also please help.

There can be any number of parameters.

I got this idea from CodeIgniter's URL handling library. But my project is not on Codeigniter so I cant use that library. Any standalone code to do that?

Thanks in advance


回答1:


Here's a function to do the job. Note that I've filled out the URLs, as parse_url needs the scheme. It will also fail on seriously malformed URLs.

function get_dispatch($url) {
    // Split the URL into its constituent parts.
    $parse = parse_url($url);

    // Remove the leading forward slash, if there is one.
    $path = ltrim($parse['path'], '/');

    // Put each element into an array.
    $elements = explode('/', $path);

    // Create a new empty array.
    $args = array();

    // Loop through each pair of elements.
    for( $i = 0; $i < count($elements); $i = $i + 2) {
        $args[$elements[$i]] = $elements[$i + 1];
    }

    return $args;
}

print_r(get_dispatch('http://www.projectname.com/module/search/param/1'));
print_r(get_dispatch('http://www.projectname.com/param/1/module/search'));

Here's the result:

Array
(
    [module] => search
    [param] => 1
)
Array
(
    [param] => 1
    [module] => search
)

However, this probably is not very robust. If there is a well written library out there that already does the job (like the one suggested by Itay Moav), then you should definitely look into it.




回答2:


Use php explode function. You will have to parse the result and see if the $result1 is a valid module in your application an so on. The logic of the parser should belong to your application. Zend_router that somebody mentioned does the same thing by trying to map to an existing module/controller/action whatever $request object it has.

$result = explode('/', $string);



回答3:


Use the PHP function parse_url http://php.net/manual/en/function.parse-url.php

$url = 'http://username:password@hostname/path?arg=value#anchor';

print_r(parse_url($url));

Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)

So you can then analyse the path section of the array.

You cold then use explode to put items into an assoc array?




回答4:


$url    = parse_url('http://www.projectname.com/module/search/param/1');
$params = explode('/', trim($url['path'], '/'));
$params = array_chunk($params, 2); //tricky part here
foreach($params as $param)
{
    $final_params[$param[0]] = $param[1];
}

edit: parse_url works with correct URLs only

array_chunk is a nice function for this task :)



来源:https://stackoverflow.com/questions/3139407/processing-url-parameters-name-value-pairs-separated-by-slashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!