I am trying to create a url router in PHP, that works like django's.
The problems is, I don't know php regular expressions very well.
I would like to be able to match urls like this:
/post/5/
/article/slug-goes-here/
I've got an array of regexes:
$urls = array(
"(^[/]$)" => "home.index",
"/post/(?P<post_id>\d+)/" => "home.post",
);
The first regex in the array works to match the home page at / but I can't get the second one to work.
Here's the code I am using to match them:
foreach($urls as $regex => $mapper) {
if (preg_match($regex, $uri, $matches)) {
...
}
}
I should also note that in the example above, I am trying to match the post_id in the url: /post/5/ so that I can pass the 5 along to my method.