Wer ab und an mit PHP Code schreibt, der wird erlebt haben, dass print_r() eine fantastische Funktion ist, um komplizierte Arrays darzustellen. Leider ist sie unformatiert, sodass bei stark verschachtelten Arrays die Übersicht schnell flöten geht. Ich habe gerade an der Nike+-API herumgedoktert, und da werden einfach alle Daten eines Benutzers in einem gigantischen Objekt mit vielfacher Schachtelung ausgegeben.
Ganz großartig funktioniert dafür die Funktion wtf() von afisher8.
<?php
function wtf($var, $arrayOfObjectsToHide=null, $fontSize=11)
{
$text = print_r($var, true);
if (is_array($arrayOfObjectsToHide)) {
foreach ($arrayOfObjectsToHide as $objectName) {
$searchPattern = '#('.$objectName.' Object\n(\s+)\().*?\n\2\)\n#s';
$replace = "$1<span style=\"color: #FF9900;\">--> HIDDEN - courtesy of wtf() <--</span>)";
$text = preg_replace($searchPattern, $replace, $text);
}
}
// color code objects
$text = preg_replace('#(\w+)(\s+Object\s+\()#s', '<span style="color: #079700;">$1</span>$2', $text);
// color code object properties
$text = preg_replace('#\[(\w+)\:(public|private|protected)\]#', '[<span style="color: #000099;">$1</span>:<span style="color: #009999;">$2</span>]', $text);
echo '<pre style="font-size: '.$fontSize.'px; line-height: '.$fontSize.'px;">'.$text.'</pre>';
}
// example usage:
wtf($myBigObject, array('NameOfObjectToHide_1', 'NameOfObjectToHide_2'));
?>
