Skip to content

Instantly share code, notes, and snippets.

@JBlond
Created December 15, 2017 14:20
Show Gist options
  • Save JBlond/d8080b362cb6dfddd11ee306d4de0c21 to your computer and use it in GitHub Desktop.
Save JBlond/d8080b362cb6dfddd11ee306d4de0c21 to your computer and use it in GitHub Desktop.
print_r with color
<?php
function print_r_color($object, $die = false){
# insert span tags
$output = '';
$output = '<span class="die_value">'. $output;
$output = str_replace('[', '<span class="die_key">[\'', print_r($object,TRUE));
$output = str_replace(']', '\']</span>', $output);
$output = str_replace('=> ', '=> <span class="die_value">', $output);
# temporarily swap these paterns
$output = str_replace(")\n\n", ")#br##br#", $output);
$output = str_replace(")\n", ")#br#", $output);
$output = str_replace("(\n", "(#br#", $output);
# close spans at remaining line breaks
$output = str_replace("\n", "</span>\n", $output);
# revert temporary swaps
$output = str_replace(")#br##br#", ")\n\n", $output);
$output = str_replace(")#br#", ")\n", $output);
$output = str_replace("(#br#", "(\n", $output);
echo '<style type="text/css">#die_object { font-size: 11px; padding: 10px; background: #eee; font-family: monospace; white-space: pre;} .die_key { color: #e00;} .die_value { color: #00e;}</style>';
if($die){
die('<div id="die_object">'.$output.'</div>');
}
else {
echo('<div id="die_object">'.$output.'</div>');
}
}
function pr($object, $die = false){
print_r_color($object, $die);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment