<?php
/**
* 将print_r处理后的数据还原为原始数组
* Date: 2016-10-31
* Author: fdipzone
* Ver: 1.0
*/
class RestorePrint{ // class start
public $res = array();
protected $dict = array();
protected $buf = '';
protected $keyname = '';
protected $stack = array();
public function __construct() {
$this->stack[] =& $this->res;
}
public function __call($method, $param){
echo $this->buf .' not defined mehtod:'.$method. ' param:'.implode(',', $param);
}
public 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;
}
public 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'])){
$arr = explode(',', $p['val']);
call_user_func_array(array($this, array_shift($arr)), $arr);
return $i;
}
return $t;
}
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;
break;
}
$this->buf = '';
}
} // class end
?>
<?php
require 'RestorePrint.class.php';
$print_r_data = <<<TXT
Array
(
[name] => fdipzone
[gender] => male
[age] => 18
[profession] => programmer
[detail] => Array(
[grade] => 1
[addtime] => 2016-10-31
)
)
TXT;
// 显示打印的数据
echo '显示打印的数据<br>';
echo '<pre>'.$print_r_data.'</pre>';
$oRestorePrint = new RestorePrint;
$oRestorePrint->set('Array', 'group');
$oRestorePrint->set(' [', 'brackets,[');
$oRestorePrint->set('] => ', 'brackets,]');
$oRestorePrint->set(')', 'brackets,)');
$oRestorePrint->parse($print_r_data);
$result = $oRestorePrint->res;
echo '还原为数组<br>';
var_dump($result);
?>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有