By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,643 Members | 1,797 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,643 IT Pros & Developers. It's quick & easy.

how to avoid eval()

P: n/a
I have a string, $cookiestring, which typically looks like this:
"us=1!uk=2!de=13!fr=8!". I want to use this to get values into an array
$r, so that $r['us']=1, $r['uk']=2, etc.

Here is how I have achieved it:
preg_match_all( "/(..)=(\d+)!/", $cookiestring, $res );
for ( $i=0 ; $i<sizeof( $res[1] ) ; $i++ ) {
eval( "$" . "r[\"" . $res[1][$i] . "\"]=". $res[2][$i] . ";" );
}

I think my use of eval is pretty horrible, and feel that there must be a
better way. But I can't get anything else to work. Can anyone advise?

Nick
--
Nick Wedd ni**@maproom.co.uk
Jan 25 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Nick Wedd" <ni**@maproom.co.ukwrote in message
news:nj**************@maproom.demon.co.uk...
>I have a string, $cookiestring, which typically looks like this:
"us=1!uk=2!de=13!fr=8!". I want to use this to get values into an array
$r, so that $r['us']=1, $r['uk']=2, etc.

foreach(explode('!', $cookiestring) as $pair){
list($key,$val) = explode('=',$pair);
$$key = $val;
}

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Jan 25 '07 #2

P: n/a
Rik
On Thu, 25 Jan 2007 11:48:13 +0100, Nick Wedd <ni**@maproom.co.ukwrote:
I have a string, $cookiestring, which typically looks like this:
"us=1!uk=2!de=13!fr=8!". I want to use this to get values into an array
$r, so that $r['us']=1, $r['uk']=2, etc.

Here is how I have achieved it:
preg_match_all( "/(..)=(\d+)!/", $cookiestring, $res );
for ( $i=0 ; $i<sizeof( $res[1] ) ; $i++ ) {
eval( "$" . "r[\"" . $res[1][$i] . "\"]=". $res[2][$i] . ";" );
}

I think my use of eval is pretty horrible, and feel that there must bea
better way. But I can't get anything else to work. Can anyone advise?
1. Regular Expression Route:

preg_match_all('/([^!=]+)=([^!=]+)!/',$cookiestring,$matches,PREG_PATTERN_ORDER);
$r = array_combine($matches[1],$matches[2]);

2. Exploding Route:

$values = explode('!',$cookiestring);
array_walk($values,create_function('$v,$k,&$array' ,'list($key,$value) =
explode(\'=\',$v);$array[$key] = [$values];));

Both untested, so there may be some typing errors.
--
Rik Wasmus
* I'm testing several new newsreaders at the moment. Please excuse
possible errors and weird content. *
Jan 25 '07 #3

P: n/a
"Kimmo Laine" <sp**@outolempi.netwrote in message
news:5q*****************@reader1.news.saunalahti.f i...
"Nick Wedd" <ni**@maproom.co.ukwrote in message
news:nj**************@maproom.demon.co.uk...
>>I have a string, $cookiestring, which typically looks like this:
"us=1!uk=2!de=13!fr=8!". I want to use this to get values into an array
$r, so that $r['us']=1, $r['uk']=2, etc.


foreach(explode('!', $cookiestring) as $pair){
list($key,$val) = explode('=',$pair);
$$key = $val;
Whoops! this should be $r[$key] = $val;
}


--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Jan 25 '07 #4

P: n/a
Nick Wedd wrote:
I have a string, $cookiestring, which typically looks like this:
"us=1!uk=2!de=13!fr=8!". I want to use this to get values into an
array $r, so that $r['us']=1, $r['uk']=2, etc.

Here is how I have achieved it:
preg_match_all( "/(..)=(\d+)!/", $cookiestring, $res );
for ( $i=0 ; $i<sizeof( $res[1] ) ; $i++ ) {
eval( "$" . "r[\"" . $res[1][$i] . "\"]=". $res[2][$i] . ";" );
}

I think my use of eval is pretty horrible, and feel that there must
be a better way. But I can't get anything else to work. Can anyone
advise?
This approach might work, too:

$cs_pairs = explode("!", $cookiestring);
foreach ($cs_pairs as $pair) {
$vals = explode("=", $pair);
$r[$vals[0]] = $vals[1];
}

--
Kim André Akerø
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Jan 25 '07 #5

P: n/a
In message <op***************@misant.kabel.utwente.nl>, Rik
<lu************@hotmail.comwrites
>1. Regular Expression Route:

preg_match_all('/([^!=]+)=([^!=]+)!/',$cookiestring,$matches,PREG_PATTER
N_ORDER);
$r = array_combine($matches[1],$matches[2]);

2. Exploding Route:

$values = explode('!',$cookiestring);
array_walk($values,create_function('$v,$k,&$array ','list($key,$value) =
explode(\'=\',$v);$array[$key] = [$values];));
Great, many thanks to you and to Kimmo Laine!

Nick
--
Nick Wedd ni**@maproom.co.uk
Jan 25 '07 #6

P: n/a
..oO(Nick Wedd)
>I have a string, $cookiestring, which typically looks like this:
"us=1!uk=2!de=13!fr=8!". I want to use this to get values into an array
$r, so that $r['us']=1, $r['uk']=2, etc.
Another one:

parse_str(strtr($cookiestring, array('!' ='&')), $r);

Micha
Jan 25 '07 #7

P: n/a
Rik
On Thu, 25 Jan 2007 18:10:45 +0100, Michael Fesser <ne*****@gmx.dewrote:
.oO(Nick Wedd)
>I have a string, $cookiestring, which typically looks like this:
"us=1!uk=2!de=13!fr=8!". I want to use this to get values into an array
$r, so that $r['us']=1, $r['uk']=2, etc.

Another one:

parse_str(strtr($cookiestring, array('!' ='&')), $r);
D'oh! Nice one.

I thought about parse_str(), but then decided it couldn't be used because
of the '!'... The simple solution of replacing them just wouldn't op into
my head :)

--
Rik Wasmus
* I'm testing several new newsreaders at the moment. Please excuse
possible errors and weird content. *
Jan 25 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.