0

たとえば、次の文字列があります

$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)("([^"]+)")/

4番目の括弧の値をgrepしたい($ 4)

4

1 に答える 1

2

一致させようとしているものに正確に依存しますが、以下はすべての一致を返します-

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

$aMatches[1]すべての一致の配列が含まれています。一致する例-$5$ 10 $ 20($と任意の長さの数値のみに一致します)

于 2012-07-22T08:49:33.023 に答える