#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2007
    Posts
    14
    Rep Power
    0

    Smile A var_dump() & print_r() alternative


    Hey guys, I couldn't find a snippet sub-forum, so I guess I'll post it here. I put together dump_r(), which I feel is a much better print_r() & var_dump() combination. It's lightweight, single-file-include, fully css-themeable and extendable for dumping additional object types. It has other features as well (see the repo link). Try it out if you'd like. I'd appreciate bug reports, feature requests, css themes, patches or general feedback. Requires PHP 5.3+, if you're on <5.3, consider this your public shaming.

    demo: http://o-0.me/dump_r/
    repo: https://github.com/leeoniya/dump_r.php

    there are also some additional expand/collapse features if you hold Ctrl/Shift while collapsing and expanding. They're briefly explained in the commit message here .

    thanks!

    sample output:

    Comments on this post

    • ManiacDan agrees
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2007
    Posts
    14
    Rep Power
    0

    Thumbs up


    [Update] now installable through Composer

    https://packagist.org/packages/leeoniya/dump-r

    require: "leeoniya/dump-r": "dev-master"
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    One thing this doesn't do is gracefully degrade on the command line. If I print_r to a non-browser (flat file, cli) it's going to be hell to try to chunk through your HTML. If $_SERVER['HTTP_USER_AGENT'] isn't set, maybe fall back to a plaintext version.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2007
    Posts
    14
    Rep Power
    0
    yeah, it'll mess your command line up!

    internally, the structure decomposition is separate from the rendering pass. so it's just a matter of writing an additional text rendering func.

    the most obvious and lightweight thing that comes to mind for text dumping is to just call var_dump and maybe reformat it slightly. there's no escaping the need for all the extra clutter when you can't rely on colors. of course this path would bypass lots of goodies that the decomposed tree offers.

    ultimately, there needs to be a text version. i'm a bit busy now so probably wont get around to it for some time. but feel free to submit pull requests

    thanks for the feedback!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2007
    Posts
    14
    Rep Power
    0
    thinking a bit more...

    once the text render method is done, the console part should be easy. but there's still no way to do something like print_r does, with explicitly returning the txt representation to write to a file.

    i might change the signature a bit to account for this in conjunction with limiting the recursion depth. maybe

    PHP Code:
    dump_r($val$expand=1000$depth=1000$ret=false); 
    if $ret is set to true, it can be assumed that text is needed perhaps...
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2007
    Posts
    14
    Rep Power
    0
    ^^ this is now implemented ^^

    as a stop-gap until i have time to write a text renderer, the lib will now fall back to plain var_dump() when in cli or when returning output.

    Comments on this post

    • ManiacDan agrees
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2007
    Posts
    14
    Rep Power
    0
    okay, the issue was annoying me, so I found some time for this. there are some small caveats and unfinished things still on the todo list.

    - multiline strings will get chopped off after the first line by the regex that handles the re-alignment after everything is buffered.
    - private and protected properties are not identified as such
    - keys are not enclosed in anything (for readability), so leading and trailing whitespace in string keys will not be obvious. i'm assuming this is pretty rare.

    there aren't options right now to limit what does and doesnt get displayed, but the planned settings are:
    - independently toggle str lengths, arr lengths, subtypes, parenths around lengths + subtypes, enclosing of string keys in single quotes
    - key => value dots as horiz guides for easier readability
    - more detail for recursion and reference types

    that said, i tried to make it as clean and useful as possible and match the html version when possible. recursion depth limiting is supported the same way as in the html output. the var_dump and print_r versions are also included at the bottom of the example.

    http://o-0.me/dump_r/example2.php

    [UPDATE] if you want to try it out, it's up on a separate branch: https://github.com/leeoniya/dump_r.php/tree/renderTEXT

    leon
    Last edited by Leeoniya; December 4th, 2012 at 07:30 PM. Reason: mention renderTEXT branch
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2007
    Posts
    14
    Rep Power
    0
    the text-mode multiline issue (the only serious one) has been fixed. unlike var_dump and print_r, it also aligns the string's individual lines to the correct nesting level.

    i merged the renderTEXT branch into master, so this functionality is now official.

    the example at http://o-0.me/dump_r/ has also been updated to include text dumps.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2007
    Posts
    14
    Rep Power
    0
    [Update] pretty significant rewrite (OOP/PSR-0). better extensibility, dumping of uniform arrays of objects or assoc arrays as record tables.

IMN logo majestic logo threadwatch logo seochat tools logo