I would use a negative lookahead ( ?! ) For this:
$urls = array( 'http://example.com/foo', 'http://example.com/foo/', 'http://example.com/foo/bar', 'http://example.com/foobar' ); foreach ($urls as $url) { if (preg_match('#^http://example\.com/foo(?!bar)#', $url)) { echo $url, " matches.\n"; } else { echo $url, " does NOT match.\n"; } } // Output: // http://example.com/foo matches. // http://example.com/foo/ matches. // http://example.com/foo/bar matches. // http://example.com/foobar does NOT match.
source share