Your code is rather confusing and misleading and if run it, it outputs a warning:
Warning: preg_match(): Unknown modifier '(' in php shell code on line 1
What I think is wrong is:
$pattern = '/foo/:any/';
#should be
$pattern = '/foo\/:any/';
because you need to escape a forward slash in regexp.
After this is fixed the script returns:
(
[0] => foo/bar/this/that
[1] => bar/this/that
)
Which is an expected result. As you match foo/
and everything afterwards with (.*)
. If you want to match anything until the next forward slash you have some possibilities:
$pattern = '/foo/(.*?)/' #non greedy
$pattern = '/foo/([^\/]*)/' #not matching any forward slash
$pattern = '@foo/:any/@' #or using different start and end markers, e.g. @