简体   繁体   中英

Regular Expression take a part of a string PHP

For example I have the following string

$s = "asd$5asd";

how can I take only the part that contains the "$5" that comes after a "d" in php we have the following

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 ???

my regex is

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

i want to grep the value of the forth brackets ($4)

Depends exactly what you are looking to match but the following will return all matches -

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

$aMatches[1] contains an array of all matches. Example matches - $5 $10 $20 (Only matches $ and a number of any length)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM