Skip to content

PHP Helpers: html_print_r

01 Feb 2010

Before I get to the next PHP Helpers function I wanted to mention that I’ve made the code available as a Google Code Project at http://code.google.com/p/php-helpers/. I added specific licensing terms ( MIT style ) – http://code.google.com/p/php-helpers/source/browse/trunk/license.txt. There’s also a changelog available to see when functions were added.

With that out of the way here’s the next PHP Helpers function, html_print_r:

if ( !function_exists( 'html_print_r' ) ) {
    function html_print_r( $data ) {
        $out = "\n<pre class='html-print-r'";
        $out .= " style='border: 1px solid #ccc; padding: 7px;'>\n";
        $out .= esc_html( print_r( $data, TRUE ) );
        $out .= "\n</pre>\n";

        return $out;
    }
}

I added this because I use PHP’s print_r in an HTML context quite a bit. To make the output look reasonable I was always adding PRE tags, which got boring fast.

Related posts:

  1. PHP Helpers: html_var_dump
  2. PHP Helpers: esc_html
  3. PHP Helpers: debug_log
  4. PHP Helpers: redirect_url
  5. PHP Helpers: make_slug

From → Posts

2 Comments
  1. my comment on the html_var_dump would probably be more appropriate here.

    printf(‘(pre)%s(/pre)’, print_r( $var, 1 ) );

    replace the () tags with lt/gt

    • Thanks. That’s a smaller version, but my intent wasn’t to make it super small, just super useful :-)

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS