Last active
July 5, 2023 13:36
-
-
Save aderowbotham/4380234 to your computer and use it in GitHub Desktop.
PHP debugging shortcut for '<pre>' + print_r() + die(), that formats keys in red and values in blue.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# colour-formatted print_r shortcut | |
if ( ! function_exists('prd')){ | |
function prd($object,$die=TRUE){ | |
# insert span tags | |
$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>'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent code. Thanks for sharing. It helped a lot.
Include a $output='' to avoid error.