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
preg_match pattern problem
Moderator: General Moderators
Re: preg_match pattern problem
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
Will return exactly what you're after.
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);
Re: preg_match pattern problem
Thanks Kadanis for the answer,
but I had tried preg_match_all
and that only returned tha last match also
but I had tried preg_match_all
and that only returned tha last match also