The PHP Print_r method allows you to print variables to make them easy to understand. If the variable is String,integer or float, the variable value itself will be printed, and if the variable is an array, the keys and elements will be displayed in a certain format. object is similar to an array. Print_r is used to print more arrays.
PHP native did not restore the Print_r method after the printed data to the original array, so write the following method, the implementation of the Print_r processed data to revert to the original array.
RestorePrint.class.php
<?php/** * Restore Print_r processed data to original array * date:2016-10-31 * author:fdipzone * ver:1.0 */class restoreprint{//Clas s start public $res = Array (); Protected $dict = Array (); protected $buf = "; protected $keyname = "; Protected $stack = Array (); Public Function __construct () {$this->stack[] =& $this->res; The Public Function __call ($method, $param) {echo $this->buf. ' 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-&G t;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?>
demo.php
<?phprequire ' RestorePrint.class.php '; $print _r_data = <<<txtarray ( [name] = Fdipzone [ Gender] = male [age] [ profession] = programmer [detail] = = Array ( [Grade] = 1
[addtime] = 2016-10-31 )) txt;//display printed data echo ' Show printed data <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 ' restore to an array <br> '; Var_dump ($result);? >
Output:
Display the printed data array ( [name] + fdipzone [gender] + male [age] + [profession] = Programmer [detail] = = Array ( [grade] = 1 [addtime] = 2016-10-31 )) restore the array array (size=5) ' name ' = > String ' fdipzone ' (length=8) ' gender ' + string ' male ' (length=4) ' age ' = String ' (length=2)
' profession ' = = String ' Programmer ' (length=10) ' detail ' = = Array (size=2) ' grade ' = = string ' 1 ' (length=1) ' addtime ' = = String ' 2016-10-31 ' (length=10)
SOURCE Download Address: Click to view
The PHP Print_r method allows you to print variables to make them easy to understand. If the variable is String,integer or float, the variable value itself will be printed, and if the variable is an array, the keys and elements will be displayed in a certain format. object is similar to an array. Print_r is used to print more arrays.
PHP native did not restore the Print_r method after the printed data to the original array, so write the following method, the implementation of the Print_r processed data to revert to the original array.
RestorePrint.class.php
<?php/** * Restore Print_r processed data to original array * date:2016-10-31 * author:fdipzone * ver:1.0 */class restoreprint{//Clas s start public $res = Array (); Protected $dict = Array (); protected $buf = "; protected $keyname = "; Protected $stack = Array (); Public Function __construct () {$this->stack[] =& $this->res; The Public Function __call ($method, $param) {echo $this->buf. ' 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; The 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-&G t;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?>
demo.php
<?phprequire ' RestorePrint.class.php '; $print _r_data = <<<txtarray ( [name] = Fdipzone [ Gender] = male [age] [ profession] = programmer [detail] = = Array ( [Grade] = 1
[addtime] = 2016-10-31 )) txt;//display printed data echo ' Show printed data <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 ' restore to an array <br> '; Var_dump ($result);? >
Output:
Display the printed data array ( [name] + fdipzone [gender] + male [age] + [profession] = Programmer [detail] = = Array ( [grade] = 1 [addtime] = 2016-10-31 )) restore the array array (size=5) ' name ' = > String ' fdipzone ' (length=8) ' gender ' + string ' male ' (length=4) ' age ' = String ' (length=2)
' profession ' = = String ' Programmer ' (length=10) ' detail ' = = Array (size=2) ' grade ' = = string ' 1 ' (length=1) ' addtime ' = = String ' 2016-10-31 ' (length=10)
The above is the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!