Re: [PHP] Eval string to array
Jochem Maas wrote: Gerard Samuel wrote: Im trying to evaluate a string representation of the output of var_export(), as an array. To make a long story short, Im using curl to fetch another php page, that uses var_export to echo out php data structures. The fetched data via curl, is a string. Something like -> array ( 'foo' => 'bar', ) something LIKE? or exactly that? anyway I don't have problems with this... php -r ' eval("\$arr = array ( \"foo\" => \"bar\", );"); $arr1 = var_export($arr, true); eval("\$arr2 = $arr1;"); var_dump($arr,$arr1,$arr2); ' ...took me a couple of mins to figure out how you could be For now, exactly like. This is what I extracted from your example for it to work -> $ret = str_replace("\n", '', curl_exec($this->curl_handle)); eval("\$arr = $ret;"); $arr1 = var_export($arr, true); var_dump($arr); I would like to convert this to a real array. Im currently trying this -> $ret = curl_exec($this->curl_handle); $str = '$array = $ret;'; this is your problems. its to do with string interpolation. your exec() call is equivelant to $array = $ret; guess what that makes $array? so it should be: exec("\$array = $ret;"); which will replace the string $ret into the string to be exec()'ed. alternatively: exec('$array = '.$ret.';'); geddit? gotit eval($str); var_dump($ret, $array); next time paste the actual var_dump output, its easier. :-) It was in the original email. Just not where you thought it would have been ;) Thanks for your help. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Eval string to array
Gerard Samuel wrote: Im trying to evaluate a string representation of the output of var_export(), as an array. To make a long story short, Im using curl to fetch another php page, that uses var_export to echo out php data structures. The fetched data via curl, is a string. Something like -> array ( 'foo' => 'bar', ) something LIKE? or exactly that? anyway I don't have problems with this... php -r ' eval("\$arr = array ( \"foo\" => \"bar\", );"); $arr1 = var_export($arr, true); eval("\$arr2 = $arr1;"); var_dump($arr,$arr1,$arr2); ' ...took me a couple of mins to figure out how you could be I would like to convert this to a real array. Im currently trying this -> $ret = curl_exec($this->curl_handle); $str = '$array = $ret;'; this is your problems. its to do with string interpolation. your exec() call is equivelant to $array = $ret; guess what that makes $array? so it should be: exec("\$array = $ret;"); which will replace the string $ret into the string to be exec()'ed. alternatively: exec('$array = '.$ret.';'); geddit? eval($str); var_dump($ret, $array); next time paste the actual var_dump output, its easier. :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Eval string to array
Im trying to evaluate a string representation of the output of var_export(), as an array. To make a long story short, Im using curl to fetch another php page, that uses var_export to echo out php data structures. The fetched data via curl, is a string. Something like -> array ( 'foo' => 'bar', ) I would like to convert this to a real array. Im currently trying this -> $ret = curl_exec($this->curl_handle); $str = '$array = $ret;'; eval($str); var_dump($ret, $array); The resulting var_dump() shows that my eval'ed data is still a string. string(27) "array ( 'foo' => 'bar', )" string(27) "array ( 'foo' => 'bar', )" What should I be doing to get it as an array -> array(1) { ["foo"]=> string(3) "bar" } Thanks for your time. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php