5

Possible Duplicate:
Create array printed with print_r

Duplicate of How create an array from the output of an array printed with print_r? which also has a nice code example to solve this

I need to reverse an error log which has as output print_r($data,true).
Example data would look like:

Array
(
    [subject] => this is the subject
    [body] => <p>Dear user,</p><p>this is the body of the email</p>
    [from_id] => 0
    [from_email] => [email protected]
    [to] => Array
        (
            [0] => Array
                (
                    [id] => 0
                    [email] => 64909
                )

        )

    [send_to_inbox] => 1
)
8
  • 3
    While not a solution - If you control the error_log call, in the future you could error_log(serialize($data)); Which can be unserialized Commented Jan 8, 2013 at 15:45
  • You could use a regex to do it Commented Jan 8, 2013 at 15:47
  • Might look into using var_export instead of print_r in the future if you keep running into this. I'm not certain you can simply "reverse" print_r because string starts/ends are ambiguous. Commented Jan 8, 2013 at 15:49
  • When you say "reverse", what format would you like to end up with? And will this code (that does the reversing) be deployed to production, or just some scaffold to help you troubleshoot a problem? (Some strategies may or may not be valid depending on what you're trying to accomplish.) Commented Jan 8, 2013 at 15:55
  • 1
    Use @hakre's script here gist.github.com/1102761 I use it and it works great. Commented Jan 8, 2013 at 15:58

2 Answers 2

12

In the PHP manual there's a print_r_reverse() function in comments : http://php.net/manual/en/function.print-r.php

However var_export() can be an alternative if your logs are generated using var_export(). This way, you only need eval() to retrieve the exported array.

Sign up to request clarification or add additional context in comments.

1 Comment

print_r_reverse() from the comments works awesome, thank you! I know it shouldn't be done this way but i really needed to parse that ****.
5

The output of print_r() is not designed to parsed; it's designed to be read by a developer for debugging purposes. You should not be trying to parse it.

If you really must parse a PHP data dump of this nature, the var_export() function is intended for this kind of thing. However, I wouldn't recommend parsing this either -- it's still unlikely to be the best solution for you.

If your intention is to store a string representation of an array structure, and then parse it later, you would be better off using either the serialize()/unserialize() functions, or the json_encode()/json_decode() functions.

Both of these will give you a much more reliable and easily parseable data dump format. Of the two, I'd recommend json_encode() every time, as not only is it easy to work with, it's also supported by other languages, easy to read manually, and compact.

In short, don't parse print_r() output; use json_encode()/json_decode() instead.

https://www.php.net/manual/en/function.json-encode.php

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.