duanmi1900 2014-05-11 14:06
浏览 213
已采纳

php正则表达式匹配单词并停止

I have a URL like this http://website.com/clothes/men/type/t-shirts/color/red/size/xl... and I need to perform an action when the url is like this http://website.com/clothes/(men or woman)/type/any-type

So if the after type/any-type there are other values I don't want to perform the action.

My regex looks like this right now preg_match('/clothes\/(men|women)\/type\/(.*)\/?$/', $_SERVER['REQUEST_URI'])

It matches the case I want, but it also matches if the URL continues after that specific key/value pair, so it also matches http://website.com/clothes/men/type/t-shirts/color/red.

So in the end I need the preg_match() to only match a URL that has only a type/anything pair.

Thank you.

  • 写回答

2条回答 默认 最新

  • dongzhan1570 2014-05-11 14:10
    关注

    You can just match [^/]+:

    preg_match('(clothes/(men|women)/type/([^/]+)/?$)', $_SERVER['REQUEST_URI'])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码