8

Doing print_r, returns pages and pages of code; it's too hard to scroll pages to match children to parents, even wrapped with <pre> tags.

Is there a way to theme print_r into collapsible fields. Maybe an online generator, where I can post the contents of the print_r($array); and get a collapsible table of fields.

For example, in Drupal, there's a module, called Devel, that does just that. Devel Screenshot

Community
  • 1
  • 1
Timofey Drozhzhin
  • 4,416
  • 3
  • 28
  • 31

4 Answers4

10

Unless I am missing something, the answer is in your screenshot: http://krumo.sourceforge.net/

EDIT (2019): Try https://github.com/kint-php/kint as it is still maintained today.

deizel.
  • 11,042
  • 1
  • 39
  • 50
  • Thanks. I was hoping it won't require any additional libraries. – Timofey Drozhzhin Oct 02 '12 at 20:37
  • Xdebug can limit the nesting level of print_r output but doesn't do collapsing, so I think you will need a PHP library like this or something on the client side (eg. some Javascript that detects raw print_r output and converts it). – deizel. Oct 02 '12 at 20:43
  • With all these CSS and JS beautifiers... can't believe something like that is not available online:/ – Timofey Drozhzhin Oct 02 '12 at 20:46
  • @deizel `print_f` (or was it `vardump`) accepts a second parameter. It the parameter (loosely; `==`) evaluates to `true`, it returns a string containing the data. You could then parse that, but it could be very slow. – Cole Tobin Apr 01 '13 at 23:28
9

Thanks to this post, here's a solution.

Insert the following function, right before the print_r.

<?php
function print_r_tree($data)
{
    // capture the output of print_r
    $out = print_r($data, true);

    // replace something like '[element] => <newline> (' with <a href="javascript:toggleDisplay('...');">...</a><div id="..." style="display: none;">
    $out = preg_replace('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iUe',"'\\1<a href=\"javascript:toggleDisplay(\''.(\$id = substr(md5(rand().'\\0'), 0, 7)).'\');\">\\2</a><div id=\"'.\$id.'\" style=\"display: none;\">'", $out);

    // replace ')' on its own on a new line (surrounded by whitespace is ok) with '</div>
    $out = preg_replace('/^\s*\)\s*$/m', '</div>', $out);

    // print the javascript function toggleDisplay() and then the transformed output
    echo '<script language="Javascript">function toggleDisplay(id) { document.getElementById(id).style.display = (document.getElementById(id).style.display == "block") ? "none" : "block"; }</script>'."\n$out";
}
?>

And then, substitute print_r(), with print_r_tree(); like this:

<pre><?php echo print_r_tree(get_defined_vars()); ?></pre>

Don't forget the <pre> tags.

The results look identical to that of print_r() function, except that now the arrays are collapsible.

Timofey Drozhzhin
  • 4,416
  • 3
  • 28
  • 31
8

Why not ouput it as JSON and then paste it on http://jsonviewer.stack.hu/, which makes it readible AND collapsible!

json_encode($array);

Example: enter image description here

edwardmp
  • 6,339
  • 5
  • 50
  • 77
1

This is the combination of the original with Christian's fix, updated due to preg_replace deprecation (PHP 7):

function print_r_tree($data)
{
    // capture the output of print_r
    $out = print_r($data, true);

    // replace something like '[element] => <newline> (' with <a href="javascript:toggleDisplay('...');">...</a><div id="..." style="display: none;">
    $out = preg_replace_callback('/([ \t]*)(\[[^\]]+\][ \t]*\=\>[ \t]*[a-z0-9 \t_]+)\n[ \t]*\(/iU', 'print_r_tree_callback', $out);

    // replace ')' on its own on a new line (surrounded by whitespace is ok) with '</div>
    $out = preg_replace('/^\s*\)\s*$/m', '</div>', $out);

    // print the javascript function toggleDisplay() and then the transformed output
    return '<script language="Javascript">function toggleDisplay(id) { document.getElementById(id).style.display = (document.getElementById(id).style.display == "block") ? "none" : "block"; }</script>'."\n$out";
}

function print_r_tree_callback($matches) {
    $id = substr(md5(rand().$matches[0]), 0, 7);
    return "$matches[1]<a href=\"javascript:toggleDisplay('$id');\">$matches[2]</a><div id='$id' style=\"display: none;\">";
}