A workaround for PHP to restore print_r processed data to the original array _php tips

Source: Internet
Author: User

The PHP Print_r method allows variables to be printed so that the variables are easy to understand. If the variable is String,integer or float, the variable value itself is printed, and if the variable is 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 to the original array, so write the following method to restore the Print_r processed data to the original array.

RestorePrint.class.php

<?php/** * Restores the Print_r processed data to the original array * date:2016-10-31 * author:fdipzone * ver:1.0 */class restoreprint{//C
  Lass 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. ' 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;
    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->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

<?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;

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 ' restores array <br> ';
Var_dump ($result);

? >

Output:

Display the printed data

Array
(
[Name] => Fdipzone
[Gender] => male
[Age] => 18
[Profession] => Programmer
[Detail] => Array (
[Grade] => 1
[Addtime] => 2016-10-31
)
)

Restore an 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 entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.