kxc*_*kxc 6 php styles colors pretty-print var-dump
如何将样式设置为var_dump()函数和PHP错误样式,如下图所示?
目前我有下一个视图var_dump()(有<pre>var_dump(...)</pre>,没有它将全部在一行)和只是纯文本的错误.
我搜索了一些PHP色彩错误,var_dump样式,但找不到任何东西.
我使用OpenServer作为localhost,在以前的版本中,我有相同的错误样式,但现在只是纯文本.自定义是真的吗?
试试这个函数.. ..覆盖var_dump()或用作不同的命名函数.我已经使用它多年了,甚至不记得它最初的来源.
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\(.*\)|\&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|\&object)\(([\w]+)\)/i', '$1(<span class="object">$2</span>)'),
// Function
'function' => array('/(^|] = )(array|string|int|float|bool|resource|object|\&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;\"> </div>".
"\n<!-- Dumpr End -->\n";
if($return) {
return $out;
} else {
echo $out;
}
}
Run Code Online (Sandbox Code Playgroud)
安装并启用Xdebug时,将获得彩色输出:
Xdebug取代了PHP的
var_dump()用于显示变量的功能。Xdebug的版本包括用于不同类型的不同颜色,并对数组元素/对象属性的数量,最大深度和字符串长度进行了限制。还有一些其他功能也可以处理变量显示。
您可以使用ini设置启用/禁用此功能 xdebug.overload_var_dump
默认情况下
var_dump(),当html_errors php.ini设置设置为1时,Xdebug重载其自身的改进版本来显示变量。如果您不希望这样做,则可以将该设置设置为0,但是请先检查是否更智能地关闭它html_errors。
检查文档以获取更多信息。
请注意,您不想在生产服务器上安装Xdebug扩展,因为它会大大降低代码执行速度。
在 xdebug 3 中没有更多的xdebug.overload_var_dump设置。当您升级到 xdebug 3 并按照 xdebug 升级指南中所述使用时xdebug.mode=debug,您将不再获得彩色输出。要获得与 xdebug 2 中相同的结果,您必须设置xdebug.mode为develop,debug
xdebug.mode=develop,debug
Run Code Online (Sandbox Code Playgroud)
当然,如果你只想要彩色输出,你只能使用develop模式,但这样你就失去了步骤调试。使用这两个选项,您可以获得彩色输出并进行单步调试。您可以根据需要指定任意数量的模式。它们必须用 分开,。所有可用模式均在此处解释https://xdebug.org/docs/all_settings#mode
| 归档时间: |
|
| 查看次数: |
4056 次 |
| 最近记录: |