preg_match pattern problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arnigudj
Forum Newbie
Posts: 2
Joined: Thu Oct 02, 2008 1:31 pm

preg_match pattern problem

Post 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
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: preg_match pattern problem

Post 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.
arnigudj
Forum Newbie
Posts: 2
Joined: Thu Oct 02, 2008 1:31 pm

Re: preg_match pattern problem

Post by arnigudj »

Thanks Kadanis for the answer,

but I had tried preg_match_all

and that only returned tha last match also
Post Reply