彩色 var_dump() 和错误

Colored var_dump() and errors

如何将样式设置为 var_dump() 函数和 PHP 错误样式,如下图所示?

目前我有 var_dump() 的下一个视图(有 <pre>var_dump(...)</pre>,没有它会全部在一行中)并且只是错误的纯文本。

我搜索了 PHP 彩色错误、var_dump 样式,但找不到任何内容。

我使用 OpenServer 作为本地主机,在以前的版本中我有相同的错误样式,但现在只是纯文本。定制是真的吗?

安装并启用时会得到彩色输出 Xdebug:

Xdebug replaces PHP's var_dump() function for displaying variables. Xdebug's version includes different colors for different types and places limits on the amount of array elements/object properties, maximum depth and string lengths. There are a few other functions dealing with variable display as well.

您可以 enable/disable 使用 ini 设置 xdebug.overload_var_dump

By default Xdebug overloads var_dump() with its own improved version for displaying variables when the html_errors php.ini setting is set to 1. In case you do not want that, you can set this setting to 0, but check first if it's not smarter to turn off html_errors.

查看文档以获取更多信息。

请注意,您不希望在生产服务器上安装 Xdebug 扩展,因为它会显着降低代码执行速度。

使用此代码。我已经使用它很多年了。我什至不记得它最初来自哪里。

function var_dump_pretty($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", "[] = ", $c);
    $c = preg_replace('/  /', "    ", $c);
    $c = preg_replace("/\"\"(.*?)\"/i", "\"\"", $c);
    $c = preg_replace("/(int|float)\(([0-9\.]+)\)/i", "() <span class=\"number\"></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", "<span class=\"string\">\"", $c);
    $c = preg_replace("/(\"\n{1,})( {0,}\})/sim", "</span>", $c);
    $c = preg_replace("/(\"\n{1,})( {0,}\[)/sim", "</span>", $c);
    $c = preg_replace("/(string\([0-9]+\) )\"(.*?)\"\n/sim", "<span class=\"string\">\"\"</span>\n", $c);

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

您也可以使用此扩展程序进行彩色调试:(这非常容易安装)

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

Symfony VarDumper is a component designed to replace your var_dumps. It performs essentially the same functionality, but provides you with much, much more information in a much prettier format. It’s the var_dump you’ve always wanted.

Xdebug 就是您要找的。 在 Ubuntu:

上安装的示例脚本

[搜索 Xdebug]

$ apt-cache search xdebug

[安装 Xdebug]

$ sudo apt-get install php-xdebug

[重启 Apache 使其工作]

$ sudo /etc/init.d/apache2 restart

在 xdebug 3 中不再有 xdebug.overload_var_dump 设置。当您升级到 xdebug 3 并按照 xdebug 升级指南中的说明使用 xdebug.mode=debug 时,您将不会再获得彩色输出。要获得与 xdebug 2 中相同的结果,您必须将 xdebug.mode 设置为 develop,debug

xdebug.mode=develop,debug

当然,如果你只想要彩色输出,你只能使用 develop 模式,但这样你就失去了调试步骤。使用这两个选项,您都可以获得彩色输出以及逐步调试。您可以指定任意多的模式 want/need。它们必须用 , 分隔。所有可用的模式都在这里解释 https://xdebug.org/docs/all_settings#mode