Reputation: 1134
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.
Upvotes: 1
Views: 268
Reputation: 23787
You can just match [^/]+
:
preg_match('(clothes/(men|women)/type/([^/]+)/?$)', $_SERVER['REQUEST_URI'])
Upvotes: 2
Reputation: 786359
You can use:
if ( preg_match('~/clothes/(?:wo)?men/type/[^/]+/$~i', $_SERVER['REQUEST_URI'], $m) ) {
// matches succeeds
}
~
to avoid escaping every forward slash.*
in the end if you don't want to match after .../type/any-type/
Upvotes: 3