String to array w/keys question

String to array w/keys question

am 11.04.2008 06:10:08 von Mike

In PHP, how can I get from:

$str = 'foo1="bar1" foo8="bar number 8" foo6="bar6"';

into

Array
(
[foo1] => bar1
[foo8] => bar number 8
[foo6] => bar6
)

I have about 2 dozen OPTIONAL parameters (foo), the $str will contain
0 or more parameters.

Thanks!

Mike

Re: String to array w/keys question

am 11.04.2008 09:23:19 von alvaroNOSPAMTHANKS

Mike escribió:
> In PHP, how can I get from:
>
> $str = 'foo1="bar1" foo8="bar number 8" foo6="bar6"';
>
> into
>
> Array
> (
> [foo1] => bar1
> [foo8] => bar number 8
> [foo6] => bar6
> )

Not fully tested:


$str = 'foo1="bar1" foo8="bar number 8" foo6="bar6"';

$result = array();
if( preg_match_all('/(\w+)="(.*)"/U', $str, $matches, PREG_SET_ORDER) ){
foreach($matches as $i){
$result[ $i[1] ] = $i[2];
}
}

echo $str . "\n";
print_r($matches);
print_r($result);

?>

Re: String to array w/keys question

am 11.04.2008 17:17:58 von Mike

On Apr 11, 12:23=A0am, "=C1lvaro G. Vicario"
wrote:
> Mike escribi=F3:
>
> > In PHP, how can I get from:
>
> > =A0 =A0$str =3D 'foo1=3D"bar1" foo8=3D"bar number 8" foo6=3D"bar6"';
>
> > into
>
> > =A0 =A0Array
> > =A0 =A0(
> > =A0 =A0 =A0 =A0 =A0 =A0[foo1] =3D> bar1
> > =A0 =A0 =A0 =A0 =A0 =A0[foo8] =3D> bar number 8
> > =A0 =A0 =A0 =A0 =A0 =A0[foo6] =3D> bar6
> > =A0 =A0)
>
> Not fully tested:
>
> >
> $str =3D 'foo1=3D"bar1" foo8=3D"bar number 8" foo6=3D"bar6"';
>
> $result =3D array();
> if( preg_match_all('/(\w+)=3D"(.*)"/U', $str, $matches, PREG_SET_ORDER) ){=

> =A0 =A0 =A0 =A0 foreach($matches as $i){
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $result[ $i[1] ] =3D $i[2];
> =A0 =A0 =A0 =A0 }
>
> }
>
> echo $str . "\n";
> print_r($matches);
> print_r($result);
>
> ?>

THANK YOU.... I'll test further, but this gives me what I was looking
for. Thanks again!

Mike