2012-07-22 47 views
0

,比如我有以下字符串正則表達式接受字符串PHP的一部分

$s = "asd$5asd"; 

我怎麼可以只包含在PHP之後,「d」 來了「$ 5」的一部分,我們有以下

preg_mach //returns int number of matches 
preg_replace //returns the string with a replaced content 
//--------------------- 
preg_grep //it greps but how to grep the "$5" 
      //that comes after "d" without grepping the "d" it's self ??? 

我正則表達式是

/((coupon|enter)\x20code\x20)("([^"]+)")/ 

我希望到grep的價值第四支架($ 4)

+1

使用['preg_match_all('#d \ $([0-9])#',$ s,$ matches,PREG_PATTERN_ORDER]'] (http://us.php.net/preg_match)獲取所有匹配。 '$ matches [1]'將包含你想要的字符串的所有實例。 – DCoder 2012-07-22 08:39:54

回答

2

取決於你在尋找什麼來匹配,但下面將返回所有比賽 -

preg_match_all('/d(\$[0-9]+)/i',$s,$aMatches); 

$aMatches[1]包含了所有比賽的數組。示例匹配 - $ 5 $ 10 $ 20(僅匹配$和任意長度的數字)

+0

值得注意的是,這是對@DCoders答案的闡述。我注意到他的正則表達式不支持多個數字,例如,它不匹配$ 10 – Chris 2012-07-22 08:50:28

+0

好吧,如果我的正則表達式是'/((優惠券|輸入)\ x20code \ x20)(「([^」] +)「)/ '我怎麼能grep的第四個支架($ 4)...謝謝 – Hilmi 2012-07-22 08:59:03

+0

@ user1225246你有一個你會發送的例子字符串,你會期望返回什麼? – Chris 2012-07-22 09:04:27

相關問題