2010-09-27 95 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

相關問題