如何在 PHP 中使用 var_dump() 查看长字符串的完整内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34342777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to see full content of long strings with var_dump() in PHP
提问by Carlos2W
I have an array with some strings like
我有一个包含一些字符串的数组,例如
$array = array("string1","string2","string3");
But those strings are very long, with a length of 2000+ sometimes. So when I do
但是这些字符串很长,有时长度为 2000+。所以当我做
echo "<pre>";
var_dump($array);
echo "</pre>";
It shows me something like
它向我展示了类似的东西
string 'zzzzzzzzzzzzzzzzz '... (length = 994)
string 'yyyyyyyyyyyyyyyyy '... (length = 1287)
string 'xxxxxxxxxxxxxxxxx '... (length = 1718)
Instead of the full string. How can I see the whole content of my array? And for those who will ask, it contains HTML tags, so that's why I don't write echo $array[string];
而不是完整的字符串。如何查看数组的全部内容?对于那些会问的人,它包含 HTML 标签,所以这就是我不写的原因echo $array[string];
回答by elixenide
You are using xdebug, which overloads the default var_dump()
to give you prettier and more configurable output. By default, it also limits how much information is displayed at one time. To get more output, you should change some settings.
您正在使用 xdebug,它会重载默认值var_dump()
,为您提供更漂亮、更可配置的输出。默认情况下,它还限制一次显示的信息量。要获得更多输出,您应该更改一些设置。
Add this to the top of your script:
将此添加到脚本的顶部:
ini_set("xdebug.var_display_max_children", -1);
ini_set("xdebug.var_display_max_data", -1);
ini_set("xdebug.var_display_max_depth", -1);
From the docs:
从文档:
xdebug.var_display_max_children
Type: integer, Default value: 128
Controls the amount of array children and object's properties are shown when variables are displayed with either xdebug_var_dump(), xdebug.show_local_vars or through Function Traces.
To disable any limitation, use -1 as value.
This setting does not have any influence on the number of children that is send to the client through the Remote Debugging feature.
xdebug.var_display_max_data
Type: integer, Default value: 512
Controls the maximum string length that is shown when variables are displayed with either xdebug_var_dump(), xdebug.show_local_vars or through Function Traces.
To disable any limitation, use -1 as value.
This setting does not have any influence on the number of children that is send to the client through the Remote Debugging feature.
xdebug.var_display_max_depth
Type: integer, Default value: 3
Controls how many nested levels of array elements and object properties are when variables are displayed with either xdebug_var_dump(), xdebug.show_local_vars or through Function Traces.
The maximum value you can select is 1023. You can also use -1 as value to select this maximum number.
This setting does not have any influence on the number of children that is send to the client through the Remote Debugging feature.
xdebug.var_display_max_children
类型:整数,默认值:128
当使用 xdebug_var_dump()、xdebug.show_local_vars 或通过函数跟踪显示变量时,控制数组子项和对象属性的数量。
要禁用任何限制,请使用 -1 作为值。
此设置对通过远程调试功能发送到客户端的子项数量没有任何影响。
xdebug.var_display_max_data
类型:整数,默认值:512
控制使用 xdebug_var_dump()、xdebug.show_local_vars 或通过函数跟踪显示变量时显示的最大字符串长度。
要禁用任何限制,请使用 -1 作为值。
此设置对通过远程调试功能发送到客户端的子项数量没有任何影响。
xdebug.var_display_max_depth
类型:整数,默认值:3
当使用 xdebug_var_dump()、xdebug.show_local_vars 或通过函数跟踪显示变量时,控制数组元素和对象属性的嵌套级别数。
您可以选择的最大值为 1023。您也可以使用 -1 作为值来选择此最大值。
此设置对通过远程调试功能发送到客户端的子项数量没有任何影响。
回答by Vesaka
Sometimes, using var_dump
might be very tedious when working with long strings.
Instead outputing the result on the browser, you can use the terminal.
Another solution is to output the result on a text file using file_put_contents
or similar. Then open the file and check the result.
有时,var_dump
在处理长字符串时使用可能会非常乏味。您可以使用终端,而不是在浏览器上输出结果。另一种解决方案是使用file_put_contents
或类似的方法在文本文件上输出结果。然后打开文件并检查结果。
回答by Santy
Something like this would also display any html tags in the values:
像这样的东西也会在值中显示任何 html 标签:
foreach($array as $key=>$value) {
echo($key.':<br /><pre>'.htmlspecialchars($value).'<pre><hr>');
}