Domanda

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.

È stato utile?

Soluzione 2

You can just match [^/]+:

preg_match('(clothes/(men|women)/type/([^/]+)/?$)', $_SERVER['REQUEST_URI'])

Altri suggerimenti

You can use:

if ( preg_match('~/clothes/(?:wo)?men/type/[^/]+/$~i', $_SERVER['REQUEST_URI'], $m) ) {
   // matches succeeds
}
  1. You can use alternate delimiter like ~ to avoid escaping every forward slash
  2. Remove .* in the end if you don't want to match after .../type/any-type/
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top