Skip to content

Instantly share code, notes, and snippets.

@rutger1140
Last active September 28, 2015 17:47
Show Gist options
  • Save rutger1140/1473720 to your computer and use it in GitHub Desktop.
Save rutger1140/1473720 to your computer and use it in GitHub Desktop.
PHP helper methods for debugging; printr
<?php
/*= PHP helper functions
****************************************************************************/
// Wrapper methods
function printr($r, $s = '') {
return printRecursive($r, array('name'=>$s));
}
/**
* Prints a variable/resource recursively on screen in html pre tag
*
* @param mixed $r
* @param array options -> name and type
* @return void (prints directly to screen)
*/
function printRecursive($r, $options = array()) {
// backward compatibility
if(!is_array($options)) $options = array('name' => $options);
if(empty($options['type'])) $options['type'] = 'print_r';
// Get file, line, class, function where call to caller of caller was made
$bt = debug_backtrace();
if ($bt[1]['function'] == 'printr' OR $bt[1]['function'] == 'printv') array_shift($bt);
$fileInfo = pathinfo($bt[0]['file']);
$file = $fileInfo['basename'];
$line = $bt[0]['line'];
$function = $bt[1]['function'];
$class = isset($bt[1]['class']) ? $bt[1]['class'] : $file;
// Check if html in output is allowed
$allowHtml = true || self::__allowHtml();
$traceMessage = !$allowHtml
? "\nDEBUG INFO: called from: [" . $class . "::" . $function . "() in " . $file ." at " . $line . "]"
:"<em>Debug info, called from: <strong>" . $class . "::" . $function . "() in " . $file ." at " . $line . "</strong></em>";
$top = '';
$bottom = '';
if (!empty($options['name'])){
if (!$allowHtml){
$top = "DEBUG TITLE: " . $options['name'] . "\n";
} else {
$top = "<div style='background-color:whitesmoke;font-weight:bold;'>" . $options['name'];
$bottom = '</div>';
}
}
$top = !$allowHtml
? "\n------------------------------------------------"
. "\n" . $traceMessage . "\n" . $top
: '<div>' . $traceMessage . '</div>' . $top;
$debugData = getPrintableData($r, $options['type']);
echo $top . $debugData . "\n" . $bottom. "\n";
}
/**
* Returns formatted, escaped text
*
* @param mixed $data
* @param string $type
* @return string text
*/
function getPrintableData($data, $type = 'var_dump') {
switch($type) {
CASE 'print_r':
default:
$out = print_r($data, true);
break;
CASE 'var_dump':
ob_start();
var_dump($data);
$out = ob_get_contents();
$out = preg_replace('/=>\n +/', ' => ', $out);
ob_end_clean();
break;
}
$allowHtml = true || self::__allowHtml();
if($allowHtml) {
// escape
$out = htmlspecialchars($out);
// highlight printv parts
if('var_dump' == $type) {
$out = preg_replace('/\[&quot;.+&quot;\]/U', '<i class="printvKey">\0</i>', $out);
$out = preg_replace('/(=&gt;) ([a-z]+[^ ]+) (&quot;|{)/iU', '\1 <span class="dataType" style="color: #C33">\2</span> \3', $out);
}
// wrap in pre-tag
$out = '<pre style="text-align:left;">' . $out . '</pre>';
}
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment