<?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 print_r方法可以把变量打印显示,使变量易于理解。如果变量是string,integer或float,将打印变量值本身,如果变量是array,将会按照一定格式显示键和元素。object与数组类似。print_r用于打印数组较多。 php原生没有把print_r方法打印后的数据还原为原始数组,因此写了下面这个方法,实现将print_r处理后的数据还原为原始数组。
资源推荐
资源详情
资源评论
收起资源包目录
restoreprint.zip (2个子文件)
demo.php 670B
RestorePrint.class.php 3KB
共 2 条
- 1
资源评论
傲雪星枫
- 粉丝: 2305
- 资源: 74
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功