Categories
Posts

PHP Helpers: html_print_r

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:

[sourcecode lang=”php”]
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;
}
}
[/sourcecode]

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.