views:

230

answers:

3

Hi,

I have my server's apache configured to redirect every request into index.php, so that I can use urls like :

http://www.website.com/about/us

Now I need to parse the url in order to determine if each route exists and what to expect from it, so I can set a database table to save the routes and so my clients can edit their routes as they like them to be. Thing is, I'm not able to do the parsing so far.

Here's my code :

$uri = 'about/us';
$regex = '(page(/<action>))';
if ( ! preg_match($regex, $uri, $matches)) {
    echo 'the route doesn\'t match';
    exit();
}

It always shows up that echo message. What am I doing wrong?

edit :

The reason why I need regex is as follows. Imagine I have a url segment like :

/user/yoda

The regex here would be usefull to parse the first argument as an action or page, and the second as a key that will only allow a certain set of chars, like if I want to expect, in this case, the second paramenter only to match "_-azAZ09", something like this. In other words, the set of regex expressions would me compared to the uri in order to determine if any of them matches the uri, and determine the action to take from that on. Using the explode function would make me evaluate the uri segments later in the code, wich I'd like to avoid. If there's no match to the uri, immediatelly forward the page to a 404 one and break the operation. Don't know if I made myself clear ..

+3  A: 

Instead of using regular expressions, why not simply split the incoming URI?

$uri = 'about/us';
list($page, $action) = explode('/', $uri);

If you need more than two, simply remove the list:

$pagePath = explode('/', $uri);

But, if you insist on using regular expressions, this will do:

$regex = '#(?P<page>.*)/(?P<action>.*)#';

However, it offers you nothing special except that $matches will now contain $matches['page'] and $matches['action'] - but if you really need it, youcan use list() as shown above.


Using the explode function would make me evaluate the uri segments later in the code, wich I'd like to avoid

Instead of making sure the page is valid by it's character set, why not compile a list of valid pages lookup table and just use php's in_array which is much faster than a regular expression? Extracting the URL segments into an array will allow you to have multi-dimension arrays validated in no time.

LiraNuna
It would work if the situation was always easy, but it ain't. So I really need to use regular expressions.
yoda
Care to explain why? URL will always have `'/'`s and this seems like a suitable solution. I can't see why regular expressions to manually split the URL entry is necessary. Is the question missing info, maybe?
LiraNuna
+1 regex is expensive!
Sepehr Lajevardi
you're right, I'll update the question
yoda
I ended up figuring out what I was missing about regex, but thanks anyway! ;)
yoda
+1  A: 

I think the regex needs to be surrounded by a symbol; for example '@(page(/<action>))@'. See the examples in http://php.net/preg_match

Nicolás
+1  A: 

If you absolutely must use a regex for some reason try this:

$uri = 'about/us';
$regex = '~(?<path>[^/]+)/(?<action>.+)~';
var_dump($matches);

Which outputs:

array(5) {
  [0]=>
  string(8) "about/us"
  ["path"]=>
  string(5) "about"
  [1]=>
  string(5) "about"
  ["action"]=>
  string(2) "us"
  [2]=>
  string(2) "us"
}

However this will only work when the path component only goes down one level!

pygorex1