ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940
Rep:
php4: avoiding eval()
hey there,
i have written a little app to parse simple xml files into a php array,
problem is that i use eval() quite a lot. does anybody know a place where i can read up on how to avoid using eval() ?
thank you
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940
Original Poster
Rep:
perhaps i am not looking properly, but i was unable to find functions that would do following:
the following xml data should be truned into an array listed below the xml data
this works with as many levels as wanted, but the downside is the use of the eval() function.
the 'type' attributes just set the type of data we are using (quite obviouse...)
but the parsing process also checks when 'type' is not set whit which kind of data we are working (integer, bool, string)
i do not use eval for attributes. i read the xml document and generate a string which gets evaled...
if you can tell me which xml functions to use to get to the same place i would greatfull.
sorry if i am missing something on the page you linked, i had a look there for quite some time, but cannot see the array outputed the same way!
function parseTagStart($parser, $name, $attrs) { global $tagname, $type; $tagname = $name; $type = $attrs['TYPE']; }
function parseTagEnd($parser, $name) { /* dummy */ }
function parseText($parser, $data) { global $conf, $tagname; if (($data = trim($data)) == '' ) return; if ($type == 'bool') $conf[$tagname] = bool($data); elseif ($type == 'int') $conf[$tagname] = int($data); else /* string? */ $conf[$tagname] = trim($data); }
/* Read and parse the xml config file */ $filename = './config.xml'; $fp = fopen($filename, 'r') or die('Could not open xml file.'); $buf = fread($fp, filesize($filename)); fclose($fp); $P = xml_parser_create() or die('Could not create XML-parser.'); xml_set_element_handler($P, 'parseTagStart', 'parseTagEnd'); xml_set_character_data_handler($P, 'parseText'); xml_parse($P, $buf); xml_parser_free($P);
/* The array is now populate with the data from the xml file. */
/* Just print it out */ foreach ($conf as $key => $value) { print $key .' = '. $value ."<br />\n"; } ?> </body> </html>
Distribution: debian, gentoo, os x (darwin), ubuntu
Posts: 940
Original Poster
Rep:
this does not create an array representing the xml file as detailed as my app. i am not looking for a replacement app, that works half as detailed, i am looking for a way to avoid the eval() function.
dont get me wrong here, i appreciate the time and effort you are putting into helping me. all i am saying that this is not quite what i looking for.
That's true.
But I still think it is a code example that shows how to do such thing. Which is what a programming forum is for IMHO, as opposed to expecting other people to write your program for you entirely.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.