给定以下 PHP 代码:
<?php
$str = '/foo/bar/baz';
preg_match('#^(/[^/]+?)*$#', $str, $matches);
var_dump($matches);
...我得到以下输出:
array (size=2)
0 => string '/foo/bar/baz' (length=12)
1 => string '/baz' (length=4)
...但我不明白为什么。我希望每场比赛都(/[^/]+?)
被捕获到自己的组中并陷入$matches
,这样它看起来像这样:
array (size=4)
0 => string '/foo/bar/baz' (length=12)
1 => string '/foo' (length=4)
2 => string '/bar' (length=4)
3 => string '/baz' (length=4)
我错过了什么?
编辑:
如果我改用这是输出preg_match_all()
,这仍然不是我想要的:
array (size=2)
0 =>
array (size=1)
0 => string '/foo/bar/baz' (length=12)
1 =>
array (size=1)
0 => string '/baz' (length=4)