Kladskull
Kladskull

Reputation: 10742

Parsing a fancy URL to variables in PHP (/class/method/x/1/y/1/z/1/)

I am sure someone has this done already, and was hoping someone could share some code.

We have the following .htaccess file in place:

RewriteEngine on
RewriteBase /
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php

This is the same one Zend uses. We are trying to emulate the same URL structure Zend does. We would use Zend but we are way to far into our own framework.

What we want to accomplish is parse a url that looks like this:

http://www.test.com/a/b/x/1/y/1/z/1

to this:

$class = 'a';
$method = 'b';
$x = 1;
$y = 1;
$z = 'a';

I can code the final solution to determine strings from numerics, but was hoping someone had already done this and is willing to share.

Upvotes: 1

Views: 647

Answers (1)

Gumbo
Gumbo

Reputation: 655329

When you already have the URL’s path, you can do this:

$path = '/a/b/x/1/y/1/z/1';
$segments = explode('/', trim($path, '/'));
for ($i=0, $n=count($segments); $i<$n-1; ++$i) {
    ${$segments[$i]} = $segments[++$i];
}

Upvotes: 5

Related Questions