2010-09-27 47 views
0

比赛是否有可能使文字的两场比赛 - /123/123/123?edit两个或更多的表达

我需要匹配123123123edit

对于第(123123123):模式是 - ([^\/]+)
对于第二(编辑):模式是 - ([^\?=]*$)

是否有可能在一个preg_match_all功能相匹配,或者我需要做两次 - 1吨ime为一种模式,第二种为第二种?

谢谢!

回答

1

您可以用单一preg_match_all调用做到这一点:

$string = '/123/123/123?edit'; 
$matches = array(); 
preg_match_all('#(?<=[/?])\w+#', $string, $matches); 

/* $matches will be: 
Array 
(
    [0] => Array 
     (
      [0] => 123 
      [1] => 123 
      [2] => 123 
      [3] => edit 
     ) 

) 
*/ 

http://www.ideone.com/eb2dy

模式((?<=[/?])\w+)在操作中查看使用lookbehind断言,斜线或一个问号必须在字符序列之前(\wshorthand class相当于​​)。

+0

太棒了!谢啦! – 2010-09-27 05:18:54