Jump to content

preg_match and foreach


bouwob

Recommended Posts

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
Share on other sites

...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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.