如何在正斜杠之后获得文本(直到下一个正斜杠或字符串结束)


How to get text after forward slashes (up until next forward slash or end of string)?

我有一个URL清理看起来像这样:

/foo/bar

我试过了:

preg_match('#([a-zA-z0-9]+)#', $path, $matches);

但是$匹配输出为:

Array
(
    [0] => /foo
    [1] => foo
)

可以使用什么正则表达式(即与preg_match())来获得foobar ?

你最好使用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'
)
相关文章: