Connecting Tech Pros Worldwide Forums | Help | Site Map

preg_match

MB
Guest
 
Posts: n/a
#3: Jun 2 '08

re: preg_match


I want to be able to extract key/value pairs from a string but I am not
succeeding. Experimenting and googling for a few hours didn't get me
anywhere so I'm hoping for help here. My input string could look
something like this:

some_var=yellow another_var = "blue and red" third_var= 'pink'

The values could be enclosed in single quotes, double quotes or no
quotes at all as you can see. Is it possible to make a regular
expression to extract these and if so, how?

Thanks
/MB

pritaeas
Guest
 
Posts: n/a
#1: Jun 2 '08

"MB" <no@mail.comwrote in message
news:yNFMj.6040$R_4.4506@newsb.telia.net...
Quote:
>I want to be able to extract key/value pairs from a string but I am not
>succeeding. Experimenting and googling for a few hours didn't get me
>anywhere so I'm hoping for help here. My input string could look something
>like this:
>
some_var=yellow another_var = "blue and red" third_var= 'pink'
>
The values could be enclosed in single quotes, double quotes or no quotes
at all as you can see. Is it possible to make a regular expression to
extract these and if so, how?
>
Thanks
/MB
Looks like HTML attributes, so here's a link:

http://haacked.com/archive/2004/10/2...matchhtml.aspx


Alexey Kulentsov
Guest
 
Posts: n/a
#2: Jun 2 '08

re: preg_match


MB wrote:
Quote:
I want to be able to extract key/value pairs from a string but I am not
succeeding. Experimenting and googling for a few hours didn't get me
anywhere so I'm hoping for help here. My input string could look
something like this:
>
some_var=yellow another_var = "blue and red" third_var= 'pink'
>
The values could be enclosed in single quotes, double quotes or no
quotes at all as you can see. Is it possible to make a regular
expression to extract these and if so, how?
>
<?php


$a='some_var=yellow another_var = "blue and red" third_var= \'pink\'';

// simple regexp without quoting
preg_match_all('/(\w+) *=
*(\w+|(?:(["\'])([^"\']*)\\3))/',$a,$res,PREG_SET_ORDER);

foreach($res as $r)
{
echo $r[1].' = '.(isset($r[4])?$r[4]:$r[2])."\n";
}

?>
Captain Paralytic
Guest
 
Posts: n/a
#4: Jun 2 '08

re: preg_match


On 14 Apr, 09:50, MB <n...@mail.comwrote:
Quote:
I want to be able to extract key/value pairs from a string but I am not
succeeding. Experimenting and googling for a few hours didn't get me
anywhere so I'm hoping for help here. My input string could look
something like this:
>
some_var=yellow another_var = "blue and red" third_var= 'pink'
>
The values could be enclosed in single quotes, double quotes or no
quotes at all as you can see. Is it possible to make a regular
expression to extract these and if so, how?
>
Thanks
/MB
Why not start a bit further back. How dod you end up with such a
string in the first place?
MB
Guest
 
Posts: n/a
#5: Jun 2 '08

re: preg_match


Captain Paralytic skrev:
Quote:
On 14 Apr, 09:50, MB <n...@mail.comwrote:
Quote:
>I want to be able to extract key/value pairs from a string but I am not
>succeeding. Experimenting and googling for a few hours didn't get me
>anywhere so I'm hoping for help here. My input string could look
>something like this:
>>
>some_var=yellow another_var = "blue and red" third_var= 'pink'
>>
>The values could be enclosed in single quotes, double quotes or no
>quotes at all as you can see. Is it possible to make a regular
>expression to extract these and if so, how?
>>
>Thanks
>/MB
>
Why not start a bit further back. How dod you end up with such a
string in the first place?
That's something I can't do anything about unfortunatly. I have to live
with how this string looks and just have to solve it somehow.

/MB
MB
Guest
 
Posts: n/a
#6: Jun 2 '08

re: preg_match


pritaeas skrev:
Quote:
"MB" <no@mail.comwrote in message
news:yNFMj.6040$R_4.4506@newsb.telia.net...
Quote:
>I want to be able to extract key/value pairs from a string but I am not
>succeeding. Experimenting and googling for a few hours didn't get me
>anywhere so I'm hoping for help here. My input string could look something
>like this:
>>
>some_var=yellow another_var = "blue and red" third_var= 'pink'
>>
>The values could be enclosed in single quotes, double quotes or no quotes
>at all as you can see. Is it possible to make a regular expression to
>extract these and if so, how?
>>
>Thanks
>/MB
>
Looks like HTML attributes, so here's a link:
I should have thought of that. :-)
I got that one to work well with some small adjustments. Thanks!

/MB

MB
Guest
 
Posts: n/a
#7: Jun 2 '08

re: preg_match


Alexey Kulentsov skrev:
Quote:
MB wrote:
Quote:
>I want to be able to extract key/value pairs from a string but I am
>not succeeding. Experimenting and googling for a few hours didn't get
>me anywhere so I'm hoping for help here. My input string could look
>something like this:
>>
>some_var=yellow another_var = "blue and red" third_var= 'pink'
>>
>The values could be enclosed in single quotes, double quotes or no
>quotes at all as you can see. Is it possible to make a regular
>expression to extract these and if so, how?
>>
>
<?php
>
>
$a='some_var=yellow another_var = "blue and red" third_var= \'pink\'';
>
// simple regexp without quoting
preg_match_all('/(\w+) *=
*(\w+|(?:(["\'])([^"\']*)\\3))/',$a,$res,PREG_SET_ORDER);
>
foreach($res as $r)
{
echo $r[1].' = '.(isset($r[4])?$r[4]:$r[2])."\n";
}
>
?>
That one works great. Just what i needed. Thanks!

/MB
Closed Thread