0

I am using eval to create an associative array from php built in xml parser (not a fan of how the array is setup). The particular XML I am using is a response from First Data and the tags and attributes both have colons which the eval function seems to really dislike.

I have tried escaping the colons but eval is mad about the slashes (and escaped slashes). What would be a good way to go about prepping the strings for the eval function?

function get_XML_Array($XML) {

    $error = true ;

    $parser = xml_parser_create() ;

    xml_parse_into_struct($parser, $XML, $set) ;

    xml_parser_free($parser);

    include_once(DIR_ROOT . "Tools/escape_Colon.php") ;

    if($error){echo_Array($set);}

    foreach($set as $key => $value) {
        foreach($set[$key] as $key_2 => $value_2){
            $set[$key][$key_2] = escape_Colon($value_2) ;
            foreach($set[$key]['attributes'] as $key_3 => $value_3){
                $set[$key]['attributes'] = escape_Colon($value_3) ;
            }
        }
    }

    $Array = array();
    $arr_str = '$Array' ;
    $arr_str_i = '$Array' ;
    $inc = 0 ;

    $level = 1 ;

    foreach($set as $key => $value) {
        if($set[$key]['level'] >= $level){
            $arr_str_i .= '[\''.$set[$key]['tag'].'\']' ;
            if(!preg_match('/^(\s)*$/', $set[$key]['value'])){
                $str = '$inc = '.$arr_str_i.'[\'increment\'] ;' ;
                eval($str) ;
                $str = $arr_str_i.'[\'value\'][\''.($inc?$inc:0).'\'] = \''.$set[$key]['value'].'\';' ;
                eval($str) ;
                $str = $arr_str_i.'[\'increment\']++ ;' ;
                eval($str) ;
            }
                foreach($set[$key]['attributes'] as $att_key => $att_value){
                    $str = $arr_str_i.'[\'attributes\'][\''.$att_key.'\'] = \''.$att_value.'\';' ;
                    eval($str) ;
                }

            if($set[$key+1]['level'] >= $level){
                $arr_str = $arr_str_i ;
            }
            }elseif($set[$key]['level'] == 1){
            $arr_str = '$Array' ;
            $arr_str_i = '$Array' ;
            $level = 1 ;
            $arr_str_i .= '['.$set[$key]['tag'].']' ;           
        }else{
            $level = $set[$key]['level'] ;
            $arr_str_i = $arr_str ;
        }

    $level++ ;
    }

    return $Array ;
}

?>
5
  • 3
    Why not just parse the xml and traverse the dom? Commented Jun 2, 2011 at 18:00
  • 1
    There is no reason (well, hardly) to ever use eval. (eval=evil) Commented Jun 2, 2011 at 18:08
  • 2
    It comes from JS, but it applies here: eval is evil, there's no reason you can't doing this directly instead of through eval. Commented Jun 2, 2011 at 18:08
  • Ok .. that code does not make any sense to me at all. What are you trying to do? Parse the XML to PHP? Why not use one of the built in XML parsers? Commented Jun 2, 2011 at 18:09
  • @GolezTrol @Rudu +1 I completely agree - the use of eval() in this script seems to be completely unnecessary. Commented Jun 2, 2011 at 18:14

2 Answers 2

2

Do not use eval(). As far as I see, everything from eval() can be replaced with the proper PHP code. For example:

$str = $arr_str_i.'[\'attributes\'][\''.$att_key.'\'] = \''.$att_value.'\';' ;
eval($str) ;

can be replaced with:

$Array['attributes'][$att_key] = $att_value;

so why do you want to complicate your life too much? IDE will help you (eg. by showing data types) if you just follow the usual, best way.

Sign up to request clarification or add additional context in comments.

3 Comments

The problem is $arr_str_i changes the value according to what array key foreach is currently on. Is there a way to turn $arr_str_i into the associated variable name? Cuz then this would be a good solution.
@Magic I see that actually the code in my answer does not correspond to what you are doing, but this is the outcome of using eval() - the visibility suffers. Just structure your code in a better way and you will achieve what you need. But never use eval() in such cases, please. And yes, go back to using "simple PHP" instead of complicating the code unnecessarily.
@Magic I think that the solution to your question from new version of the comment is as simple as $arr_str_i = $arr_str[$set[$key]['tag']]; :) Or similar - the question is not specific enough.
0

It comes from JS, but it applies here: eval is evil, there's no reason you can't doing this directly instead of through eval.

On the other hand it sounds like the features of SimpleXML in php would be a better choice for you see simplexml_load_string.

3 Comments

I think the answer is in this somewhere but I really dont know where (yet). Hopefully I can dig something up in simpleXML.
Well simpleXML will transform XML into an object in a structure similar to what you're doing above... I think, as a few have said it's a tad tricky to complete understand ;)
Yea i think the reason its a pain to understand is because xml_parse_into_struct creates such a weird array. I really didnt want to bog the question down with a post of an example but Im really sure you guys would get it pretty quick if you could see an example array :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.