Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reverse print_r result

Posted on 2014-02-14
4
Medium Priority
?
764 Views
Last Modified: 2016-10-13
Ive got a string contiaing the results from a print_r by using:-
     $myDump = print_r($_SERVER,true);

Open in new window


However now I need to convert $myDump back into an Array, so something like:-
     $myArray = toArray($myDump);

Open in new window


So in the end $myArray should be an exact replica of what $_Server was when I issues print_r ????

So my question is any ideas how I can convert the string

Array
(
    [radQ1] => Yes
    [radQ1_Comments] => Comments...
)
to an array?
0
Comment
Question by:tonelm54
  • 2
4 Comments
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39858672
Just use the $_SERVER array! You haven't changed it.

If you need to save the $_SERVER array for later, then just save that in a variable:

$myServerArray = $_SERVER;
$myDump = print_r($myServerArray, true);
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39858755
If you need to save the $_SERVER array for later,
... then you have a very funky code smell!  In fact, you have a code stench.

But I expect the question is not really about $_SERVER, but about any general print_r() output -- can it be un-munged and turned into the original data set?  I've wondered about this myself since many questions here at EE contain the output of print_r() as a representation of the test data, and I have often had to spend some time converting that back into a PHP variable.

I found this "script kiddie hack" example some time ago and put it into my untested library.  It looked interesting, but I have never tried to document or test it.  It might provide a germ of an idea.  I believe it is supposed to reverse var_dump() output which seems feasible.  It would be harder, I think, to reverse print_r() because you do not have all of the data type representations and would have to make some assumptions.

<?php // RAY_un_var_dump.php

// UNTESTED

function unvar_dump($str) {
    if (strpos($str, "\n") === false) {
        //Add new lines:
        $regex = array(
            '#(\\[.*?\\]=>)#',
            '#(string\\(|int\\(|float\\(|array\\(|NULL|object\\(|})#',
        );
        $str = preg_replace($regex, "\n\\1", $str);
        $str = trim($str);
    }
    $regex = array(
        '#^\\040*NULL\\040*$#m',
        '#^\\s*array\\((.*?)\\)\\s*{\\s*$#m',
        '#^\\s*string\\((.*?)\\)\\s*(.*?)$#m',
        '#^\\s*int\\((.*?)\\)\\s*$#m',
        '#^\\s*float\\((.*?)\\)\\s*$#m',
        '#^\\s*\[(\\d+)\\]\\s*=>\\s*$#m',
        '#\\s*?\\r?\\n\\s*#m',
    );
    $replace = array(
        'N',
        'a:\\1:{',
        's:\\1:\\2',
        'i:\\1',
        'd:\\1',
        'i:\\1',
        ';'
    );
    $serialized = preg_replace($regex, $replace, $str);
    $func = create_function(
        '$match',
        'return "s:".strlen($match[1]).":\\"".$match[1]."\\"";'
    );
    $serialized = preg_replace_callback(
        '#\\s*\\["(.*?)"\\]\\s*=>#',
        $func,
        $serialized
    );
    $func = create_function(
        '$match',
        'return "O:".strlen($match[1]).":\\"".$match[1]."\\":".$match[2].":{";'
    );
    $serialized = preg_replace_callback(
        '#object\\((.*?)\\).*?\\((\\d+)\\)\\s*{\\s*;#',
        $func,
        $serialized
    );
    $serialized = preg_replace(
        array('#};#', '#{;#'),
        array('}', '{'),
        $serialized
    );

    return unserialize($serialized);
}

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39858866
Please see http://www.laprbass.com/RAY_temp_tonelm54.php

This is only a starting point -- it would need a lot of work to deal with things like multidimensional arrays and objects.

<?php // RAY_temp_tonelm54.php
error_reporting(E_ALL);
echo '<pre>';

// THE ORIGINAL PRINT_R() OUTPUT IN A STRING
$str = <<<EOD
Array
(
    [radQ1] => Yes
    [radQ1_Comments] => Comments...
)
EOD;

// SHOW THE STARTING POINT
echo htmlentities($str);
echo PHP_EOL;

// PROCESS EACH LINE OF THE PRINT_R()
$new = array();
$arr = explode(PHP_EOL, $str);
foreach ($arr as $sub)
{
    if (strpos($sub, '['))
    {
        // ISOLATE THE KEY
        preg_match('#\[(.*?)\]#', $sub, $mat);
        $key = $mat[1];

        // AND THE VALUE
        preg_match('#\=\> (.*?)$#', $sub, $mat);
        $val = $mat[1];

        // ADD TO THE ARRAY
        $new[$key] = $val;
    }
}

// WHAT JUST HAPPENED
print_r($new);
echo PHP_EOL;

Open in new window

0
 

Expert Comment

by:Ondrej Letacek
ID: 41841854
Hi,
There is a print_r_reverse function described on http://php.net/manual/en/function.print-r.php
Alternatively, you can look at online tool on https://www.nestforms.com/php-print_r-to-json-online where you can convert the print_r result into the json (which you can import into PHP by json_decode).
1

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question