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
Janice [ Mo, 14 April 2008 11:50 ] [ ID #1942181 ]

Re: preg_match

On 14 Apr, 09:50, MB <n... [at] mail.com> wrote:
> 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?
Captain Paralytic [ Mo, 14 April 2008 11:58 ] [ ID #1942182 ]

Re: preg_match

Captain Paralytic skrev:
> On 14 Apr, 09:50, MB <n... [at] mail.com> wrote:
>> 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
Janice [ Mo, 14 April 2008 12:04 ] [ ID #1942183 ]

Re: preg_match

"MB" <no [at] mail.com> wrote in message
news:yNFMj.6040$R_4.4506 [at] newsb.telia.net...
>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/25/usingregularexpression stomatchhtml.aspx
pritaeas [ Mo, 14 April 2008 12:38 ] [ ID #1942184 ]

Re: preg_match

MB wrote:
> 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";
}

?>
Alexey Kulentsov [ Mo, 14 April 2008 13:14 ] [ ID #1942185 ]

Re: preg_match

Alexey Kulentsov skrev:
> MB wrote:
>> 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
Janice [ Mo, 14 April 2008 13:52 ] [ ID #1942189 ]

Re: preg_match

pritaeas skrev:
> "MB" <no [at] mail.com> wrote in message
> news:yNFMj.6040$R_4.4506 [at] newsb.telia.net...
>> 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. :-)

> http://haacked.com/archive/2004/10/25/usingregularexpression stomatchhtml.aspx

I got that one to work well with some small adjustments. Thanks!

/MB
Janice [ Mo, 14 April 2008 13:53 ] [ ID #1942190 ]
PHP » comp.lang.php » preg_match

Vorheriges Thema: session not working on a php page
Nächstes Thema: How to update PHP5 on webserver