Created
October 3, 2014 16:16
-
-
Save xeoncross/64077db877701816969f to your computer and use it in GitHub Desktop.
Simple PHP object dump for those large objects with huge blobs of text or deep nested structures
This file contains hidden or 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
<?php | |
/** | |
* Simple object dump for those large objects with huge blobs of text or deep nested structures | |
*/ | |
function objectDump($object, $size = 70, $max = 5, $level = 0) | |
{ | |
if($level > $max) return 'array(...MAX DEPTH REACHED...)'; | |
$tab = str_repeat("\t", $level); | |
$safe = function($string) use(&$size) { | |
$string = htmlspecialchars($string, ENT_QUOTES, 'utf-8'); | |
$string = preg_replace("~\n\s*~", ' ', str_replace("\r", '', $string)); | |
if(strlen($string) > $size) { | |
return substr($string, 0, $size) . '...'; | |
} | |
return $string; | |
}; | |
$temp = array_flip(array_keys( (array) $object)); | |
foreach((array) $object as $key => $value) { | |
if( ! is_scalar($value) AND $value) { | |
$temp[$key] = objectDump($value, $size, $max, $level + 1); | |
continue; | |
} | |
$temp[$key] = ($value === NULL ? 'null' : $safe($value)); | |
} | |
if($level === 0) { | |
print "<pre>\n" . print_r($temp, true) . "</pre>\n"; | |
return; | |
} | |
return $temp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get **Warning: htmlspecialchars() expects parameter 1 to be string, array given in /home/x/y/app/code/XXX/Shipping/Helper/Quote.php on line 232