note 87705 added to function.print-r

From: Date: Wed, 17 Dec 2008 16:53:47 +0000
Subject: note 87705 added to function.print-r
Groups: php.notes 
Request: Send a blank email to php-notes+get-148274@lists.php.net to get a copy of this message
I use this all the time when debugging objects, but when you have a very large object with big arrays of sub-objects, it's easy to get overwhelmed with the large amount of output....sometimes you don't want to see absolutely every sub-object. I made this function to debug objects while "hiding" sub-objects of certain types. This also color codes the output for a more readable printout. <?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;\">--&gt; HIDDEN - courtesy of wtf() &lt;--</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')); ?> ---- Server IP: 208.69.120.35 Probable Submitter: 70.182.248.219 ---- Manual Page -- http://www.php.net/manual/en/function.print-r.php Edit -- https://master.php.net/note/edit/87705 Del: integrated -- https://master.php.net/note/delete/87705/integrated Del: useless -- https://master.php.net/note/delete/87705/useless Del: bad code -- https://master.php.net/note/delete/87705/bad+code Del: spam -- https://master.php.net/note/delete/87705/spam Del: non-english -- https://master.php.net/note/delete/87705/non-english Del: in docs -- https://master.php.net/note/delete/87705/in+docs Del: other reasons-- https://master.php.net/note/delete/87705 Reject -- https://master.php.net/note/reject/87705 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#148274) next »