Skip to content

Instantly share code, notes, and snippets.

@LastDreamer
Last active February 26, 2019 08:48
Show Gist options
  • Save LastDreamer/d7d7ca841c276ab8bf42 to your computer and use it in GitHub Desktop.
Save LastDreamer/d7d7ca841c276ab8bf42 to your computer and use it in GitHub Desktop.
hl - syntax highlight print_r analog
<?php
/**
* hl - syntax highlight print_r analog
* @author Oleg Kravchenko <lex69@list.ru>
*/
static function hl($value, $return = false)
{
// color_theme for hl
ini_set('highlight.string', '#99CF50');
ini_set('highlight.keyword', '#F8F8F8');
ini_set('highlight.default', '#E28964');
ini_set('highlight.comment', '#99CF50');
$highlight_bg = '#222222';
// styles & button
$id = preg_replace("/\d/", '', md5(rand()));
$output = '<style>#'.$id.'{background:'.($highlight_bg!=''?$highlight_bg:'transparent').'; padding:20px;}'
.'#'.$id.'>code>span>span:first-child{display:none;}'
.'.hidden{display:none;}code{word-wrap:break-word;}</style>'
.'<button id="'.$id.'1">Show/Hide</button>';
if ( is_array($value) || is_object($value) )
{
// quotes
function add_slashes(&$var)
{
switch (true) {
// object - search strings
case is_object($var) || is_array($var):
foreach ($var as &$v)
add_slashes($v);
break;
// adding quotes
default:
$var = "'".str_replace("'", "`", $var)."'";
break;
}
}
add_slashes($value);
// convert to string
$value_str = print_r($value, true);
$d_c = ini_get('highlight.default');
$k_c = ini_get('highlight.keyword');
// output
$output .= '<div id="'.$id.'">'.preg_replace("/\[(class|default)\]/",
"[</span><span style='color:".$d_c
.";'>$1</span><span style='color:".$k_c.";'>]",
highlight_string('<?php '.$value_str, true)).'</div>';
}
else
$output .= '<div id="'.$id.'">'.highlight_string($value, true).'</div>';
// open/hide script
$output .= '<script>function tgl(el){el.style.display = (el.style.display == "none")?"block":"none"}'
.'document.getElementById("'.$id.'1").addEventListener("click",'
.'function(){tgl(document.getElementById("'.$id.'"))});</script>';
// return or view
if ( $return == true )
return $output;
elseif ( $return == 'die' )
die ($output);
else
echo $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment