bouwob Posted February 23, 2007 Share Posted February 23, 2007 if (preg_match('/([a-zA-Z]+DM)/', $pieces[3], $matches)) { foreach($matches as $match) { $dm = $dm.$match.","; echo $dm; } } B-series, 00, B18C5, Integra Type R USDM this is the data that comes in. but the variable prints out "USDM,USDM" when it should print out only 1. There could be more than 1 match though. such as B-series, 00, B18C5, Integra Type R JDM USDM EDM and that should make $dm = "JDM,USDM,EDM" any ideas tia Link to comment https://forums.phpfreaks.com/topic/39746-preg_match-and-foreach/ Share on other sites More sharing options...
effigy Posted February 23, 2007 Share Posted February 23, 2007 ...the variable prints out "USDM,USDM" when it should print out only 1. Per the documentation: If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on. There could be more than 1 match though...and that should make $dm = "JDM,USDM,EDM" Also from the docs: preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject. Link to comment https://forums.phpfreaks.com/topic/39746-preg_match-and-foreach/#findComment-191984 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.