Page 1 of 1

preg_match pattern problem

Posted: Fri Oct 03, 2008 5:16 am
by arnigudj
I have a string that looks like this:
(asfd=asdf,qwer=qwew,ery=errtui)

and I want to store all value=value in an array using pregmatch like so:

preg_match($pattern,$string,$array);

what pattern could I use??


I tried '/(\w+)=(\w+)/' but that only returns:

Array
(
[0] => ery=errtui
[1] => ery
[2] => errtui
)

but I would like to get:
Array
(
[0] =>{
[0] => asfd=asdf
[1] => asfd
[2] => asdf
}
[1] =>{
[0] => qwer=qwew
[1] => qwer
[2] => qwew
}
[2] =>{
[0] => ery=errtui
[1] => ery
[2] => errtui
}
)


please help... THANX

Re: preg_match pattern problem

Posted: Fri Oct 03, 2008 9:17 am
by Kadanis
Just a note, but I think this will probably be moved into the Regular Expression specific forum if a moddy comes along ;)

Anyway, on with the answer, you're almost there tbh. All you need is to use preg_match_all instead of preg_match along with the PREG_SET_ORDER flag

Code: Select all

 
$pattern = '/(\w+)=(\w+)/';
$string  = '(asfd=asdf,qwer=qwew,ery=errtui)';
 
preg_match_all($pattern,$string,$array, PREG_SET_ORDER);
 
Will return exactly what you're after.

Re: preg_match pattern problem

Posted: Fri Oct 03, 2008 11:02 am
by arnigudj
Thanks Kadanis for the answer,

but I had tried preg_match_all

and that only returned tha last match also