我有一个URL清理看起来像这样:
/foo/bar
我试过了:
preg_match('#([a-zA-z0-9]+)#', $path, $matches);
但是$匹配输出为:
Array
(
[0] => /foo
[1] => foo
)
可以使用什么正则表达式(即与preg_match()
)来获得foo
和bar
?
你最好使用explosion:
print_r(explode('/',ltrim('/foo/bar','/')));
但是如果你想用regex来做(对于这个非常糟糕),只需使用preg_match_all()
:
preg_match_all('/([a-zA-Z0-9]+)/','/foo/bar', $matches);
echo '<pre>';
print_r($matches[0]);
更多信息请访问:http://nl3.php.net/manual/en/function.preg-match-all.php结果都是:
Array
(
[0] => foo
[1] => bar
)
使用PHP爆炸():
$arr = explode('/', substr($str,1) );
返回Array(
[0] = 'foo';
[1] = 'bar'
)