还原print_r($arr, TURE) 变成字串,如何还原
//还原print_r() 数组 $arr = array('C'=>'ccc', 'D'=>'ddd'); $p = print_r($arr,TRUE); echo '<pre>'; var_dump($arr); var_dump($p); echo '</pre>'; 如何还原?
//还原方法 class Trie { protected $dict = array(); protected $buf = ''; function set($word, $value='') { if(is_array($word)) foreach($word as $k=>$v) $this->set($k, $v); $p =& $this->dict; foreach(str_split($word) as $ch) { if(! isset($p[$ch])) $p[$ch] = array(); $p =& $p[$ch]; } $p['val'] = $value; return $this; } function parse($str) { $this->doc = $str; $this->len = strlen($str); $i = 0; while($i < $this->len) { $t = $this->find($this->dict, $i); if($t) { $i = $t; $this->buf = ''; }else $this->buf .= $this->doc{$i++}; } } protected function find(&$p, $i) { if($i >= $this->len) return $i; $t = 0; $n = $this->doc{$i}; if( isset($p[$n]) ) $t = $this->find($p[$n], $i+1); if($t) return $t; if( isset($p['val']) ) { $ar = explode(',', $p['val']); call_user_func_array( array($this, array_shift($ar)), $ar ); return $i; } return $t; } function __call($method, $param) { echo "****\n$this->buf 未定义方法:$method 参数:" . join(',', $param) . "<br />\n"; } } class App extends Trie { public $res = array(); protected $stack = array(); protected $keyname = ''; protected $buf = ''; function __construct() { $this->stack[] =& $this->res; } protected function group() { if(! $this->keyname) return; $cnt = count($this->stack) - 1; $this->stack[$cnt][$this->keyname] = array(); $this->stack[] =& $this->stack[$cnt][$this->keyname]; $this->keyname = ''; } protected function brackets($c) { $cnt = count($this->stack) - 1; switch($c) { case ')': if($this->keyname) $this->stack[$cnt][$this->keyname] = trim($this->buf); $this->keyname = ''; array_pop($this->stack); break; case '[': if($this->keyname) $this->stack[$cnt][$this->keyname] = trim($this->buf); break; case ']': $this->keyname = $this->buf; } $this->buf = ''; } } $arr=array('abcd'=>"sdfasdf",'bbb'=>'lxg','ccc'=>'bbbbbbbbb'); $ar = array('a'=>$arr, 'b'=>$arr); $s = print_r($ar,true); $p = new App; $p -> set('Array', 'group') -> set(' [', 'brackets,[') -> set('] => ', 'brackets,]') -> set(')', 'brackets,)'); $p->parse($s); print_r($p->res);