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

我想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 回答