【问题标题】:Colored var_dump() and errors彩色 var_dump() 和错误
【发布时间】:2015-10-24 15:32:51
【问题描述】:

如何将样式设置为 var_dump() 函数和 PHP 错误样式,就像下一张图片一样?

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

我搜索了一些 PHP 彩色错误,var_dump 样式,但找不到任何东西。

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

【问题讨论】:

  • 谷歌:PHP XDebug 第一个结果。
  • 你好 Rizier123!是的,我用谷歌搜索了一些关于它的东西,发现它已经在我的 localhost - php.ini 中启用了。如果我为他们设置我的 IDE,它会以某种方式影响我的浏览器样式输出吗?
  • 你确定你已经正确安装了吗?然后您是否也重新启动了服务器?
  • 你是对的 Rizier123。对不起,我的错误,一行被评论了。一切正常,谢谢!

标签: php colors styles pretty-print var-dump


【解决方案1】:

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

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", "[$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;
    }
}

【讨论】:

    【解决方案2】:

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

    Xdebug 替换了 PHP 的 var_dump() 函数来显示变量。 Xdebug 的版本包括针对不同类型的不同颜色,并对数组元素/对象属性的数量、最大深度和字符串长度进行了限制。还有一些其他函数也处理变量显示。

    您可以使用 ini 设置 xdebug.overload_var_dump 启用/禁用此功能

    默认情况下,当 html_errors php.ini 设置为 1 时,Xdebug 会使用自己的改进版本重载 var_dump() 以显示变量。如果您不希望这样,可以将此设置设置为 0,但首先检查是否关闭 html_errors 并不聪明。

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

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

    【讨论】:

    • 如果你安装了它,也永远不要忘记restart the server :)
    • 谢谢,这就是我要找的。​​span>
    【解决方案3】:

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

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

    Symfony VarDumper 是一个旨在替换您的 var_dumps 的组件。它执行基本相同的功能,但以更漂亮的格式为您提供更多信息。这是您一直想要的 var_dump。

    【讨论】:

      【解决方案4】:

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

      xdebug.mode=develop,debug
      

      当然,如果您只想要彩色输出,您只能使用develop 模式,但您会丢失单步调试。使用这两个选项,您可以获得彩色输出和单步调试。您可以根据需要/需要指定任意数量的模式。它们必须用, 分隔。所有可用的模式都在这里解释https://xdebug.org/docs/all_settings#mode

      【讨论】:

        【解决方案5】:

        Xdebug 是您正在寻找的。 在 Ubuntu 上安装的示例脚本:

        [搜索 Xdebug]

        $ apt-cache search xdebug
        

        [安装 Xdebug]

        $ sudo apt-get install php-xdebug
        

        [重启 Apache 使其工作]

        $ sudo /etc/init.d/apache2 restart
        

        【讨论】:

          猜你喜欢
          • 2019-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-28
          • 1970-01-01
          • 2019-09-17
          相关资源
          最近更新 更多