473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to avoid eval()

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
7 1906
"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
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
"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
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
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
..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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Reply Via Newsgroup | last post by:
This might sound sad... someone requesting a disertation on the 'eval' statement... but... I've been reading someone else's post - they had a huge calander like script and a handful of folk cursed...
11
by: sneill | last post by:
I have read a number of posts on the use of eval() in Javascript, and I agree that its use is questionable. But it does beg the following question: "How arbitrary does a string need to be before...
7
by: David Laub | last post by:
I have stumbled across various Netscape issues, none of which appear to be solvable by tweaking the clientTarget or targetSchema properties. At this point, I'm not even interested in "solving"...
15
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
3
by: Pauljh | last post by:
Hi All, I'm running some javascript over a server side generated web page and have multiple generated empty select statements, that I want to populate when the page is loaded. As HTML doesn't do...
2
by: Carlos Aguayo | last post by:
Hi, Is there a better way to do this? The problem that I have is that x can be "true" or "false" (as type string), or true or false (as boolean type). I'm doing comparisons like... if (eval(x)...
6
by: Good Man | last post by:
Hi there I am trying to assign an "onclick" event to a dynamically-created DOM element (an image). I have read countless times that "eval()" is to be avoided at all costs. However, I cannot...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
10
by: Gordon | last post by:
I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); objType is a select with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.