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] => admin@karakas.org
[to] => Array
(
[0] => Array
(
[id] => 0
[email] => 64909
)
)
[send_to_inbox] => 1
)
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.
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.