Colored var_dump () and errors

How to set styling to var_dump()

and styling PHP like in the following image?

enter image description here

enter image description here

At the moment I have the following view var_dump()

(with <pre>var_dump(...)</pre>

, without it everything will be on one line) and just a text for errors.

enter image description here

I searched for something for PHP color errors, styles var_dump

, but couldn't find anything.

I used OpenServer as localhost and in the previous version I had the same styles for errors, but now just plain text. Can you customize?

+3


source to share


4 answers


You get colored output when you install and enable Xdebug :

Xdebug replaces PHP's function var_dump()

for displaying variables. The Xdebug version includes different colors for different types and limits the number of array elements / object properties, maximum line depth and length. There are several more functions related to variable display.

You can enable / disable this via ini setting xdebug.overload_var_dump



By default, Xdebug overloads var_dump()

its own improved version to display variables when the html_errors php.ini parameter is set to 1. If you don't want that, you can set this parameter to 0, but first check if it's not smarter to disable html_errors.

Check the documentation for more information.

Note that you do not want the Xdebug extension installed on the production server, as this will slow down the code execution significantly.

+2


source


Try this function .... override var_dump()

or use like another named function. I've been using it for years, I can't even remember where it came from.



function var_dump($data, $label='', $return = false) {

$debug           = debug_backtrace();
$callingFile     = $debug[0]['file'];
$callingFileLine = $debug[0]['line'];

ob_start();
var_dump($data);
$c = ob_get_contents();
ob_end_clean();

$c = preg_replace("/\r\n|\r/", "\n", $c);
$c = str_replace("]=>\n", '] = ', $c);
$c = preg_replace('/= {2,}/', '= ', $c);
$c = preg_replace("/\[\"(.*?)\"\] = /i", "[$1] = ", $c);
$c = preg_replace('/  /', "    ", $c);
$c = preg_replace("/\"\"(.*?)\"/i", "\"$1\"", $c);
$c = preg_replace("/(int|float)\(([0-9\.]+)\)/i", "$1() <span class=\"number\">$2</span>", $c);

// Syntax Highlighting of Strings. This seems cryptic, but it will also allow non-terminated strings to get parsed.
$c = preg_replace("/(\[[\w ]+\] = string\([0-9]+\) )\"(.*?)/sim", "$1<span class=\"string\">\"", $c);
$c = preg_replace("/(\"\n{1,})( {0,}\})/sim", "$1</span>$2", $c);
$c = preg_replace("/(\"\n{1,})( {0,}\[)/sim", "$1</span>$2", $c);
$c = preg_replace("/(string\([0-9]+\) )\"(.*?)\"\n/sim", "$1<span class=\"string\">\"$2\"</span>\n", $c);

$regex = array(
    // Numberrs
    'numbers' => array('/(^|] = )(array|float|int|string|resource|object\(.*\)|\&amp;object\(.*\))\(([0-9\.]+)\)/i', '$1$2(<span class="number">$3</span>)'),
    // Keywords
    'null' => array('/(^|] = )(null)/i', '$1<span class="keyword">$2</span>'),
    'bool' => array('/(bool)\((true|false)\)/i', '$1(<span class="keyword">$2</span>)'),
    // Types
    'types' => array('/(of type )\((.*)\)/i', '$1(<span class="type">$2</span>)'),
    // Objects
    'object' => array('/(object|\&amp;object)\(([\w]+)\)/i', '$1(<span class="object">$2</span>)'),
    // Function
    'function' => array('/(^|] = )(array|string|int|float|bool|resource|object|\&amp;object)\(/i', '$1<span class="function">$2</span>('),
);

foreach ($regex as $x) {
    $c = preg_replace($x[0], $x[1], $c);
}

$style = '
/* outside div - it will float and match the screen */
.dumpr {
    margin: 2px;
    padding: 2px;
    background-color: #fbfbfb;
    float: left;
    clear: both;
}
/* font size and family */
.dumpr pre {
    color: #000000;
    font-size: 9pt;
    font-family: "Courier New",Courier,Monaco,monospace;
    margin: 0px;
    padding-top: 5px;
    padding-bottom: 7px;
    padding-left: 9px;
    padding-right: 9px;
}
/* inside div */
.dumpr div {
    background-color: #fcfcfc;
    border: 1px solid #d9d9d9;
    float: left;
    clear: both;
}
/* syntax highlighting */
.dumpr span.string {color: #c40000;}
.dumpr span.number {color: #ff0000;}
.dumpr span.keyword {color: #007200;}
.dumpr span.function {color: #0000c4;}
.dumpr span.object {color: #ac00ac;}
.dumpr span.type {color: #0072c4;}
';

$style = preg_replace("/ {2,}/", "", $style);
$style = preg_replace("/\t|\r\n|\r|\n/", "", $style);
$style = preg_replace("/\/\*.*?\*\//i", '', $style);
$style = str_replace('}', '} ', $style);
$style = str_replace(' {', '{', $style);
$style = trim($style);

$c = trim($c);
$c = preg_replace("/\n<\/span>/", "</span>\n", $c);

if ($label == ''){
    $line1 = '';
} else {
    $line1 = "<strong>$label</strong> \n";
}

$out = "\n<!-- Dumpr Begin -->\n".
    "<style type=\"text/css\">".$style."</style>\n".
    "<div class=\"dumpr\">
    <div><pre>$line1 $callingFile : $callingFileLine \n$c\n</pre></div></div><div style=\"clear:both;\">&nbsp;</div>".
    "\n<!-- Dumpr End -->\n";
if($return) {
    return $out;
} else {
    echo $out;
}
}

      

+5


source


You can also use this extension for color debugging: (it's very easy to install)

http://www.sitepoint.com/var_dump-introducing-symfony-vardumper/

Symfony VarDumper is a component designed to replace your var_dumps. It does almost the same functionality, but provides much more information in a much better format. Its var_dump you've always wanted.

enter image description here

+1


source


Xdebug is what you are looking for. Examples of scripts for installation on Ubuntu:

[Search for Xdebug]

$ apt-cache search xdebug

      

[Install Xdebug]

$ sudo apt-get install php-xdebug

      

[Restart Apache to get it working]

$ sudo /etc/init.d/apache2 restart

      

+1


source







All Articles