我想在PHP中创建一个url路由器,就像django的一样。PHP preg_match url的一部分
问题是,我不太了解php正则表达式。
我希望能够匹配这样的网址:
/post/5/
/article/slug-goes-here/
我有正则表达式的数组:
$urls = array(
"(^[/]$)" => "home.index",
"/post/(?P<post_id>\d+)/" => "home.post",
);
数组中的第一个正则表达式的工作相匹配的家页面/
但我不能得到第二个工作。
下面是我使用,以配合它们的代码:
foreach($urls as $regex => $mapper) {
if (preg_match($regex, $uri, $matches)) {
...
}
}
我也应该注意到,在上面的例子中,我试图在URL中post_id
匹配:/post/5/
,这样我可以通过5
沿着我的方法。
正则表达式应该有适当的分隔符 – stillstanding 2010-11-18 05:37:09