You are here: Re: how to avoid eval() « PHP Programming Language « IT news, forums, messages
Re: how to avoid eval()

Posted by Kim Andrι Akerψ on 01/25/07 11:41

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ΓΈ
- kimandre@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация