【问题标题】:PHP url routing regexPHP url 路由正则表达式
【发布时间】:2012-07-30 15:32:15
【问题描述】:

我正在使用正则表达式来路由我的应用程序,这是我正在使用的正则表达式:

@/users/([a-zA-Z0-9\-\_]+)/posts@

但不幸的是,它也与这些网址匹配:

/users/:uid/posts/:pid
/users/:uid/posts/:pid/comment/:cid

但它不应该,它应该匹配完全相同的网址,所以只有:

/users/:uid/posts

我应该在正则表达式中更改什么以使其匹配完全相同的字符串?

感谢帮助

【问题讨论】:

    标签: php regex


    【解决方案1】:

    您应该在字符串的开头 (^) 和结尾 ($) 中包含 anchors

    @^/users/([a-zA-Z0-9\-\_]+)/posts/?$@
    

    我还允许在 URL 末尾添加可选的 /

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-10
      • 2013-10-22
      • 1970-01-01
      • 2020-10-10
      • 2019-01-01
      • 2014-04-14
      • 2012-09-23
      相关资源
      最近更新 更多