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

Posted by Rik on 01/25/07 11:26

On Thu, 25 Jan 2007 11:48:13 +0100, Nick Wedd <nick@maproom.co.uk> wrote=
:

> I have a string, $cookiestring, which typically looks like this:
> "us=3D1!uk=3D2!de=3D13!fr=3D8!". I want to use this to get values int=
o an array
> $r, so that $r['us']=3D1, $r['uk']=3D2, etc.
>
> Here is how I have achieved it:
> preg_match_all( "/(..)=3D(\d+)!/", $cookiestring, $res );
> for ( $i=3D0 ; $i<sizeof( $res[1] ) ; $i++ ) {
> eval( "$" . "r[\"" . $res[1][$i] . "\"]=3D". $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=
?

1. Regular Expression Route:

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

2. Exploding Route:

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

explode(\'=3D\',$v);$array[$key] =3D [$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. *

 

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

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация